[英]javascript : changing <DIV> onclick value
I have a <DIV>
which I'm using as a button like this : 我有一个
<DIV>
,它被用作这样的按钮 :
<div id='project_1_task_2_is_done'
class='button_style_1_small' onClick='taskIsDone(1,2,"y");'>
Is done?
</div>
I want to change the onClick string value to taskIsDone(1,2,"n")
once the user clicks on it, and then, when it clicks again, back to taskIsDone(1,2,"y")
and so on. 我想在用户单击onClick字符串值
taskIsDone(1,2,"n")
其更改为taskIsDone(1,2,"n")
,然后再次单击时,将其taskIsDone(1,2,"y")
回taskIsDone(1,2,"y")
,依此类推。
I'm trying to accomplish this from the taskIsDone javascript function such as 我正在尝试通过taskIsDone javascript函数(例如
if(is_done=='n'){
document.getElementById('project_' + project_id + '_task_' + task_id + '_is_done')
.onclick = "taskIsDone(" + project_id + "," + task_id + ",'y');"
}
but it's not working. 但它不起作用。
I looked over the net to more than 7-8 examples, most from here (Stackoverflow) but I'm not finding the answer to my question (which is basically changing dynamically the onclick string for that DIV id. I don't want to run a function, as most of the examples i found present, just want to change the onclick string value, modifying "y" to "n", and again "y", etc, for each click on the div button. 我在网上查看了7到8个以上的示例,大部分都是从这里(Stackoverflow)开始的,但是我没有找到问题的答案(基本上是在动态更改该DIV ID的onclick字符串。我不想就像我发现的大多数示例一样,运行一个函数,只是想更改onclick字符串值,对div按钮的每次单击将“ y”修改为“ n”,然后再次将“ y”修改为此类 。
thanks!! 谢谢!!
See http://jsfiddle.net/wnw0oskd/ 参见http://jsfiddle.net/wnw0oskd/
You can change it as an attribute, as the alert shows onlick
of the element is not 'taskIsDone(1,2,"y");' 你可以改变它作为一个属性,作为警报显示
onlick
的元素的不是“taskIsDone(1,2,‘Y’);” but a function. 但是一个功能。
document.getElementById('project_' + project_id + '_task_' + task_id + '_is_done').setAttribute("onclick", "taskIsDone(" + project_id + "," + task_id + ",'n'");"
If you print the current value of document.getElementById('your_div').onclick
you realize tht it's not the string taskIsDone(1,2,"y");
如果您打印
document.getElementById('your_div').onclick
的当前值,您将意识到它不是字符串taskIsDone(1,2,"y");
, but it's: ,但是:
function (event) {
taskIsDone(1,2,"y");
}
So, if you want to change the value of .onclick
, assign it one such a function. 因此,如果您想更改
.onclick
的值,请为其分配一个这样的函数。
Example (toggle the value of .onclick
between a()
and b()
): 示例(在
a()
和b()
之间切换.onclick
的值):
<html>
<body>
<script>
function a() {
alert("a()");
document.getElementById('foo').onclick = function(event) {
b();
}
}
function b() {
alert("b()");
document.getElementById('foo').onclick = function(event) {
a();
}
}
</script>
<div id="foo" style="position: absolute; left: 20px; top: 20px; width: 200px; height: 50px; background-color: red;" onclick="a();">
</div>
</body>
</html>
Try this. 尝试这个。
<div id='project_1_task_2_is_done'
class='button_style_1_small' >
Is done?
</div>
var check=false;
var button = document.getElementsByClassName('button_style_1_small');
button.click =function(){
if(check==false){
check=true;
taskIsDone(1,2,"n");
}else{
check=false;
taskIsDone(1,2,"y");
}
}
to answer your question specifically, try setting "onlick" to a function instead of a string: 要具体回答您的问题,请尝试将“ onlick”设置为函数而不是字符串:
if (is_done=='n'){
var elmt = document.getElementById(...); // arguments omitted for brevity
elmt.onclick = function(project_id, task_id) {
taskIsDone(project_id, task_id, 'y');
}
}
Having said that, though, there are better ways of doing this sort of thing. 话虽这么说,但是有更好的方法可以做这种事情。 You could, for instance keep your state in a data attribute attached to the div:
例如,您可以将状态保留在附加到div的data属性中:
<div id='project_1_task_2_is_done' data-done="no"
class='button_style_1_small' onClick='toggleDone(ev, 1, 2)'>
Is done?
</div>
<script type="text/javascript">
function toggleDone(ev, projectId, taskId) {
var elmt = ev.target
elmt.dataset.done = elmt.dataset.done == "yes" ? "no" : "yes"
}
</script>
See http://www.w3schools.com/tags/att_global_data.asp 参见http://www.w3schools.com/tags/att_global_data.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.