[英]Can I override prototype functions in IE8?
I have written following code to alert messages when Node.prototype.appendChild(obj) is called. 我编写了以下代码,以在调用Node.prototype.appendChild(obj)时提醒消息。
var _appendChild = Node.prototype.appendChild;
Node.prototype.appendChild = function(object){
alert("append");
return _appendChild.apply(this,[object]); ;
};
And it does not work in IE8.. 而且它在IE8中不起作用。
I have read this link in which the answer said prototype functions can't be overridden in IE 我已经阅读了此链接,其中答案说原型功能不能在IE中覆盖
How do I override javascript's cloneNode? 如何覆盖javascript的cloneNode?
But I still want to ask if there is any work around to do what I want. 但是我仍然想问是否有任何工作要做我想要的事情。
Thanks 谢谢
You cannot extend Node in IE8, but you can extend HTMLDocument.prototype and Element.prototype. 您不能在IE8中扩展Node,但是可以扩展HTMLDocument.prototype和Element.prototype。
A link to Microsoft's documentation Microsoft文档的链接
function _MS_HTML5_getElementsByClassName(classList){
var tokens= classList.split(" ");
var staticNodeList= this.querySelectorAll("." + tokens[0]);
for(var i= 1; i<tokens.length; i++){
var tempList= this.querySelectorAll("." + tokens[i]);
var resultList= new Array();
for(var finalIter= 0; finalIter<staticNodeList.length; finalIter++){
var found= false;
for(var tempIter= 0; tempIter<tempList.length; tempIter++){
if(staticNodeList[finalIter]== tempList[tempIter]){
found= true;
break;
}
}
if(found){
resultList.push(staticNodeList[finalIter]);
}
}
staticNodeList= resultList;
}
return staticNodeList;
}
if(!document.getElementsByClassName && Element.prototype){
HTMLDocument.prototype.getElementsByClassName= _MS_HTML5_getElementsByClassName;
Element.prototype.getElementsByClassName= _MS_HTML5_getElementsByClassName;
}
Thanks kennebec 谢谢肯尼贝克
Finally I found I could not implement it just because it was running in quirks mode... I wrote an example in which somebody else may be interested . 最终,我发现我不能仅仅因为它以怪癖模式运行而无法实现它。我写了一个示例,可能有人对此感兴趣。
var elementPrototype = typeof HTMLElement !== "undefined"
? HTMLElement.prototype : Element.prototype;
var _appendChild = elementPrototype.appendChild;
elementPrototype.appendChild = function(content){
//Do what you want-----
alert("Append Child!");
//---------------------
return _appendChild(content);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.