2012-07-25 49 views
1

被覆蓋JavaScript方法我重寫了一些對Element原型方法,這樣我就可以在自定義的鉤子添加像這樣:恢復在IE8

Element.prototype._method = Element.prototype.method; 
Element.prototype.method = function(){ 
    this._method.apply(this, arguments); 
    // custom callback 
} 

在某一點上,我要恢復原來的方法,所以我做的:

Element.prototype.method = Element.prototype._method; 

然而,當method元素稱爲節點上,它似乎拋出一個Invalid procedure call or argument錯誤在IE8。我是否錯誤地恢復原始方法?

+0

+1因爲這是一個有趣的問題。但是我懷疑真正的答案是重寫代碼並以不同的方式做事。 – Spudley 2012-07-25 06:24:38

回答

0

看來IE8有這個問題,並不是很容易解決,但你可以試試deleteElement.prototype恢復覆蓋。

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 
+0

太好了!謝謝! – user730569 2012-07-25 06:49:05