appendChild = function(message) {
console.log("intercepted!");
}
上記のコードを使用してもうまくいかないようです。
誰でも知っていますか?
appendChild = function(message) {
console.log("intercepted!");
}
上記のコードを使用してもうまくいかないようです。
誰でも知っていますか?
置き換えたいと思うかもしれませんがElement.prototype.appendChild
、それはおそらく悪い考えです。
この例ではintercepted
、挿入された要素にテキストを追加します:
var f = Element.prototype.appendChild;
Element.prototype.appendChild = function(){f.apply(this, arguments);arguments[0].innerHTML="!Intercepted!"; };
document.body.appendChild(document.createElement("div"));
ネイティブ関数を上書きすることはお勧めできませんが、上書きする場合は、ネイティブの「appendChild」関数の戻り値を使用するコードの問題を防ぐために、追加された要素も返すようにしてください。
window.callbackFunc = function(elem, args) {
// write some logic here
}
window.f = Element.prototype.appendChild;
Element.prototype.appendChild = function() {
window.callbackFunc.call(this, arguments);
return window.f.apply(this, arguments);
};