我重写了 Element
原型(prototype)上的许多方法,以便我可以添加自定义 Hook ,如下所示:
Element.prototype._method = Element.prototype.method;
Element.prototype.method = function(){
this._method.apply(this, arguments);
// custom callback
}
在某些时候,我想恢复原来的方法,所以我这样做:
Element.prototype.method = Element.prototype._method;
但是,当在节点上调用 method
元素时,在 IE8 中似乎会抛出 Invalid procedure call or argument
错误。我是否错误地恢复了原来的方法?
最佳答案
看来IE8有这个问题,而且不太容易解决,但你可以尝试对Element.prototype
进行delete
来恢复覆盖。
var old = Element.prototype.getElementsByTagName;
Element.prototype.getElementsByTagName = old;
// alert(document.body.getElementsByTagName('script').length); // this throws Error
delete Element.prototype.getElementsByTagName;
alert(document.body.getElementsByTagName('script').length); // Now it works as expected
关于javascript - 在 IE8 中恢复被覆盖的 JavaScript 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11643695/