如何覆盖appendChild()?

NoW*_*een 9 javascript dom overriding

appendChild = function(message) {
    console.log("intercepted!");
}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码似乎不起作用.

有谁知道?

Den*_*ret 15

您可能想要替换的是,Element.prototype.appendChild但这可能是一个坏主意.

此示例intercepted在inserted元素中添加文本:

var f = Element.prototype.appendChild;
Element.prototype.appendChild = function(){f.apply(this, arguments);arguments[0].innerHTML="!Intercepted!"; };
document.body.appendChild(document.createElement("div"));
Run Code Online (Sandbox Code Playgroud)

示范