繁体   English   中英

我可以覆盖IE8中的原型功能吗?

[英]Can I override prototype functions in IE8?

我编写了以下代码,以在调用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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM