簡體   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