appendChild = function(message) {
console.log("intercepted!");
}使用上面的代码似乎不起作用。
有人知道吗?
发布于 2013-01-11 02:55:26
您可能想要替换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"));Demonstration
发布于 2015-03-19 20:47:31
不建议覆盖本机函数,但如果这样做,请确保同时返回附加的元素,以防止使用本机"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);
};https://stackoverflow.com/questions/14265219
复制相似问题