所以我正在为这个网站编写一个 Greasemonkey 脚本。无论如何,我的问题是我无法将 onclick 事件附加到我新创建的锚点中。
我不知道出了什么问题,也许是因为我在 Greasemonkey 这就是为什么它没有按预期工作?
function createButton() {
var a = document.createElement('a');
var css = document.createElement('style');
css.type = 'text/css';
css.innerHTML = '#prt { position:absolute; right:3em; top: 6em; font-family: Arial,Helvetica,sans-serif; font-weight:bold; font-size:125%; background: #777777 none repeat scroll 0 0; color: white; padding: 6px 12px;}'
a.href = '#';
a.innerHTML = 'Print Topic';
a.id = 'prt';
a.onclick = getTopic; // DOESN'T WORK
document.body.insertBefore(a, document.body.lastChild);
document.body.appendChild(css);
}
我尝试了其他技术,例如 setAttribute
a.setAttribute('onclick', function() { alert("hey"); });
setAttribute 也没有工作..
怎么会??
大家好,
我让它工作了。有人在 IRC #greasemonkey 上帮助了我。谢谢 broquaint!
因此,使用 setAttribute 或元素的 .onclick 属性的问题是,greasemonkey 不支持它们,而是会在 JavaScript 控制台中返回“组件不可用”错误。
如果您在使用之前遇到过这个问题
element.addEventListener ('click', myClickHandler, false);
这是维基:http ://wiki.greasespot.net/XPCNativeWrapper#Event_Handlers