使用greasemonkey 添加这两个按钮。我尝试颠倒顺序,但始终是第二个添加的按钮有效。另一个按钮不会触发 onclick 事件。
// Add the click to chat button
mydiv = document.getElementById("optionalJobinfoData");
var btn = document.createElement("input");
btn.type = 'button';
btn.value = 'Click to Chat';
btn.onclick = function() { window.open(chatURL); };
mydiv.innerHTML += "<i>Notify Dispatch</i><br><i>(stuck on job)</i> ";
mydiv.appendChild(btn);
// Credentials button
mydiv = document.getElementById("optionalJobinfoData");
btn = document.createElement("input");
btn.type = 'button';
btn.value = 'Credentials';
btn.onclick = function() { window.open(credsURL); };
mydiv.innerHTML += "<br><br><i>DSL Credentials</i> ";
mydiv.appendChild(btn);
最佳答案
问题在于执行 mydiv.innerHTML += ...
这与这里的答案类似。 Manipulating innerHTML removes the event handler of a child element?
关于javascript - 在div内添加2个按钮时,只有第二个起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14778817/