一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

getElementsByTagName vs selectNodes效率 及兼容的selectNodes實現

于是就測試了下:
復制代碼 代碼如下:
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>"),
c,
d1=new Date();
for(var i=0;i<100000;i++){
c=xmlDoc.getElementsByTagName("a");
}
document.write("getElementsByTagName: ",new Date()-d1);
d1=new Date();
try{
for(var i=0;i<100000;i++){
c=xmlDoc.selectNodes("a");
}
document.write("<br/>selectNodes: ",new Date()-d1);
}catch(ex){document.write("<br/>error:"+ex)}

在IE下selectNodes還是快多了,
可以FF下卻沒有這個方法,google了下,找了方法,使用XPathEvaluator來實現,下面是具體實現,不過效率就不太理想了:
復制代碼 代碼如下:
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;
}
})()
}

evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result);
Returns an XPathResult based on an XPath expression and other given parameters.
xpathExpression is a string representing the XPath to be evaluated.
contextNode specifies the context node for the query (see the [http://www.w3.org/TR/xpath XPath specification). It's common to pass document as the context node.
namespaceResolver is a function that will be passed any namespace prefixes and should return a string representing the namespace URI associated with that prefix. It will be used to resolve prefixes within the XPath itself, so that they can be matched with the document. null is common for HTML documents or when no namespace prefixes are used.
resultType is an integer that corresponds to the type of result XPathResult to return. Use named constant properties, such as XPathResult.ANY_TYPE, of the XPathResult constructor, which correspond to integers from 0 to 9.
result is an existing XPathResult to use for the results. null is the most common and will create a new XPathResult
完整的測試頁面:
復制代碼 代碼如下:
<!doctype HTML>
<html>
<head>
<title>selectNodes&getElementsByTagName</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="sohighthesky"/>
<meta name="Keywords" content="selectNodes vs getElementsByTagName"/>
</head>
<body>
</body>
<script type="text/Javascript">
/*
*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>"),
c,
d1=new Date();
for(var i=0;i<100000;i++){
c=xmlDoc.getElementsByTagName("a");
}
document.write("getElementsByTagName: ",new Date()-d1);
d1=new Date();
try{
for(var i=0;i<100000;i++){
c=xmlDoc.selectNodes("a");
}
document.write("<br/>selectNodes: ",new Date()-d1);
}catch(ex){document.write("<br/>error:"+ex)}
/*
var n=xmlDoc.selectSingleNode("body/a"),doc=xmlDoc.selectSingleNode("body");//alert(n.childNodes[0].nodeValue)
for(var i=0;i<10000;i++){
doc.appendChild(n.cloneNode(true))
}
d1=new Date();
c=xmlDoc.getElementsByTagName("a");
document.write("<br/>getElementsByTagName: ",new Date()-d1);
d1=new Date();
c=xmlDoc.selectNodes("a");
document.write("<br/>selectNodes: ",new Date()-d1);
*/
</script>
</html>

JavaScript技術getElementsByTagName vs selectNodes效率 及兼容的selectNodes實現,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 欧美日韩色视频在线观看 | 91精品国产高清久久久久久91 | 色哟哟小说 | 久久精品成人免费网站 | 久草五月 | 免费国产成人综合 | 影院成人区精品一区二区婷婷丽春院影视 | 不卡国产00高中生在线视频 | 国产精品亚洲一区二区三区正片 | 日韩六九视频 | 野战露脸在线视频国产 | 91福利区| 日本一区二区三区欧美在线观看 | 亚洲午夜国产精品无卡 | 成人免费精品视频 | 欧美亚洲综合另类成人 | 久久精品免费全国观看国产 | 东北普通话清晰对白 | 草久久久久 | 久国产精品久久精品国产四虎 | 免费精品美女久久久久久久久 | 国产一区国产二区国产三区 | 国产只有精品 | 国产激情视频在线播放 | 激情五月婷婷色 | 国产成人麻豆精品video | 不良网站在线观看 | 米奇色网 | 99国产精品欧美久久久久久影院 | 91热精品视频 | 看全色黄大色黄大片女图片 | 久久香蕉精品成人 | 最新国产精品自拍 | 色视频免费在线 | 91短视频在线观看免费最新91 | 免播放器在线观看 | 天天操天天透 | 久久国产成人亚洲精品影院老金 | 午夜在线播放视频 | 黄网在线观看网址入口 | 亚洲第一区二区快射影院 |