だから私はこのウェブサイトの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コンソールに「Componentnotavailable」エラーが返されます。
使用する前にこの問題が発生した場合
element.addEventListener ('click', myClickHandler, false);
これがWikiです:http ://wiki.greasespot.net/XPCNativeWrapper#Event_Handlers