我以在互联网上找到的方式动态创建了一个按钮:
Page = function(...) {
...
};
Page.prototype = {
...
addButton : function() {
var b = content.document.createElement('button');
b.onclick = function() { alert('OnClick'); }
},
...
};
不幸的是,它未能工作并抛出以下错误:
Error: [Exception... "Component is not available" nsresult: "0x80040111
(NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: chrome://knowledgizer/content
/knowledgizer.js :: <TOP_LEVEL> :: line 137" data: no]
Source File: chrome://browser/content/tabbrowser.xml Line: 434
使用 setAttribute 的解决方案有效:
b.setAttribute("onClick", "alert('OnClick')");
然而,我想调用一个类方法(而不是alert),b.onclick语法在这方面看起来更好,我希望/认为。这个onclick区分大小写吗?因为如果我写成
b.onClick = function() {alert("OnClick");} // notice the spelling onclick vs onClick
我没有收到上述错误,但它仍然无法工作,即我没有收到警报。感谢任何提示。
额外的问题:当单击按钮时,如何避免重新加载当前页面?我只想调用一个方法而不是导致页面重新加载。
谢谢和最好的问候,
Christian