Ran into this problem today, posting in case someone else has the same issue.
var execBtn = document.createElement('input');
execBtn.setAttribute("type", "button");
execBtn.setAttribute("id", "execBtn");
execBtn.setAttribute("value", "Execute");
execBtn.setAttribute("onclick", "runCommand();");
Turns out to get IE to run an onclick on a dynamically generated element, we can't use setAttribute. Instead, we need to set the onclick property on the object with an anonymous function wrapping the code we want to run.
execBtn.onclick = function() { runCommand() };
BAD IDEAS:
You can do
execBtn.setAttribute("onclick", function() { runCommand() });
本文探讨了在使用JavaScript动态创建HTML元素时,如何在IE浏览器中正确设置`onclick`属性以触发事件。通过使用匿名函数包裹目标函数来避免IE的兼容性问题。
9264

被折叠的 条评论
为什么被折叠?



