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);
};