10
appendChild = function(message) {
    console.log("intercepted!");
}

上記のコードを使用してもうまくいかないようです。

誰でも知っていますか?

4

2 に答える 2

16

置き換えたいと思うかもしれませんが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"));

デモンストレーション

于 2013-01-10T18:55:25.633 に答える
4

ネイティブ関数を上書きすることはお勧めできませんが、上書きする場合は、ネイティブの「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);
};
于 2015-03-19T12:47:31.293 に答える