我在 html 中动态添加一个按钮,如下所示: 单击该按钮我想调用一个 Javascript 函数:
var but = document.createElement("button");
but.value="delete row";
but.setAttribute("onclick","callJavascriptFunction()");
//this is not working
but.onclick="callJavascriptFunction()"
//this is also not working
document.getElementById("but").onclick="callJavascriptFunction()"
//this is also not working
but.id="but"+inc;
有什么方法可以解决这个问题吗?请帮助我。在此先感谢
原文由 sanjeev 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试这个:
或者通过用另一个元素包裹它来创建按钮并使用 innerHTML: