我编写了以下代码,以便在调用Node.prototype.appendChild(obj)时发出警报。
var _appendChild = Node.prototype.appendChild;
Node.prototype.appendChild = function(object){
alert("append");
return _appendChild.apply(this,[object]); ;
};
而且它在IE8中不起作用。
我已经阅读了这个链接,其中的答案是原型函数不能在IE中被覆盖
如何覆盖 javascript 的 cloneNode?
但我仍然想问是否有任何解决方法可以做我想做的事。
谢谢
你不能在IE8中扩展Node,但可以扩展HTMLDocument.prototype和Element.prototype。
指向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;
}
谢谢肯尼贝克
最后我发现我无法实现它,因为它以怪癖模式运行......我写了一个其他人可能感兴趣的示例.
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);
}