天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

getElementsByTagName vs selectNodes效率 及兼容的selectNodes實(shí)現(xiàn)

于是就測(cè)試了下:
復(fù)制代碼 代碼如下:
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下卻沒(méi)有這個(gè)方法,google了下,找了方法,使用XPathEvaluator來(lái)實(shí)現(xiàn),下面是具體實(shí)現(xiàn),不過(guò)效率就不太理想了:
復(fù)制代碼 代碼如下:
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
完整的測(cè)試頁(yè)面:
復(fù)制代碼 代碼如下:
<!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技術(shù)getElementsByTagName vs selectNodes效率 及兼容的selectNodes實(shí)現(xiàn),轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 臀精插宫NP文 | 国产精品久久人妻拍拍水牛影视 | 亚洲AV无码乱码国产精品品麻豆 | 暖暖 日本 视频 在线观看免费 | 理论片87福利理论电影 | 欧美一区二区VA毛片视频 | 伊人久久影院 | 国产在线视频分类精品 | 久久AV亚洲精品一区无码网 | 午夜伦理一yy4480影院 | 狠狠色狠色综合曰曰 | 99热在线精品视频 | 亚洲一区二区女搞男 | 国产午夜免费不卡精品理论片 | 天天靠天天擦天天摸 | 一区不卡二区卡 | 蜜臀AV浪潮99国产麻豆 | 亚洲人成77777在线视频 | 国产人妻777人伦精品HD | 日日摸夜添夜夜夜添高潮 | 婷婷五月久久丁香国产综合 | 国产精品AV色欲蜜臀在线 | 亚洲精品中文字幕制 | jizz教师| 黄色三级视频在线 | 伦理片97影视网 | 青青伊人精品 | 日本 稀土矿 | 最近的中文字幕免费完整 | 美女黄图大全 | z0000性欧美| 国产色婷亚洲99精品AV | 轻轻cao| 一级性生活毛片 | 亚洲在线无码免费观看 | 9999精品视频 | YELLOW视频在线观看大全 | 俄罗斯美女破处 | 伊人精品在线 | 中文字幕在线视频免费观看 | 国产亚洲精品久久久久久国模美 |