我试图使用内部html并动态设置包含变量的onclick语句:
cell5.innerHTML="<input type='button' value='Remove' onclick='removerow('manual_" + (num) + "')' />"
假设num变量设置为4.输出应为onclick="removerow('manual_4')"
任何帮助?
答案 0 :(得分:4)
最好的办法是避免这一切:
var input = document.createElement('input');
input.type = 'button';
input.value = 'Remove';
input.onclick = function() {
removerow('manual_' + num);
};
cell5.appendChild(input);
根据按钮,单元格和要删除的行之间的关系,您还可以在处理程序内执行DOM遍历以获取对该行的引用。
答案 1 :(得分:2)
您可以使用\
转义嵌套引号:
"...onclick=\"removerow('manual_" + (num) + "')\"..."
答案 2 :(得分:1)
cell5.innerHTML='<input type="button" value="Remove" onclick="removerow(\'manual_' + (num) + '\')" />'
答案 3 :(得分:0)
您可以使用\
所以
onclick=\"removerow('manual_" + (num) + "')\"
答案 4 :(得分:0)
请考虑使用点击事件。这使事情变得更加清晰:
$(".cell5") // whatever selector you want
.html('<input type="button" value="Remove"/>')
.click(function(){
removerow('manual_' + row);
});
还要考虑使用行号作为输入而不是字符串。如果它们都具有相同的形式,您只需简化:
removerow(row);
显然,这是一个API设计决策,但如果它有意义,它会简化这个表达式。