简体   繁体   English

为什么在Javascript中设置的此onclick事件不会触发?

[英]Why does this onclick event set in Javascript not fire?

The code is simple: 代码很简单:

var td1 = document.createElement("td");
td1.id="td_radio_"+nr_int+"_"+nr_var2;
td1.style.border = "0";
td1.style.width = "5%";
td1.onclick="adaugare_varianta_simplu(\'"+nr_int+"\',\'"+nr_var2+"\');";

but the function doesn't fire when I click the cell; 但是当我单击单元格时该功能不会触发; what am I doing wrong? 我究竟做错了什么? I'm not using bind because later on there's gonna be a removeAttr working on it so I want it set up as an attribute. 我不使用绑定,因为稍后将有一个removeAttr对其进行处理,因此我希望将其设置为属性。

Think you need this: 想你需要这个:

td1.onclick="function(){adaugare_varianta_simplu(\'"+nr_int+"\',\'"+nr_var2+"\');}";

You have to wrap the event in a function. 您必须将事件包装在函数中。

You are assigning a string as event handler so it can not be executed, below is more what you are after I think. 您正在将一个字符串分配为事件处理程序,因此无法执行该字符串,以下是我想的更多内容。

var td1 = document.createElement("td");
td1.id="td_radio_"+nr_int+"_"+nr_var2;
td1.style.border = "0";
td1.style.width = "5%";
td1.onclick = function() {
    adaugare_varianta_simplu(nr_int,nr_var2);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM