Firefox selectNodes

/*
 *author:sohighthesky -- http://www.cnblogs.com/sohighthesky
 *content: selectNodes vs getElementsByTagName 
 */
 
if (!window.ActiveXObject) {
(function(){
    var oEvaluator=new XPathEvaluator(),oResult;
    XMLDocument.prototype.selectNodes = function(sXPath) {
        oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
        var aNodes = [];
        if (oResult != null) {
            var oElement = oResult.iterateNext();
            while (oElement) {
                aNodes[aNodes.length]=oElement;
                oElement = oResult.iterateNext();
            }
        }
        return aNodes;
    }
    XMLDocument.prototype.selectSingleNode = function(sXPath) {
        oResult = oEvaluator.evaluate(sXPath, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
        // FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
        return oResult==null?null:oResult.singleNodeValue;
    }
})()
}
 
var stringToDom=function(text) {
    var doc;
    if(window.ActiveXObject) {
        doc = new ActiveXObject("MSXML2.DOMDocument");
        doc.loadXML(text).documentElement;
    } else {
        doc = (new DOMParser()).parseFromString(text,"text/xml");
    }
    return doc;
}
var xmlDoc=stringToDom("<body><a href='a'>a</a><a href='b'>b</a></body>"),


出自: http://www.cnblogs.com/sohighthesky/archive/2010/02/26/getelementsbytagname-selectnodes.html

你可能感兴趣的:(firefox)