天天躁日日躁狠狠躁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)系我們修改或刪除,多謝。

主站蜘蛛池模板: 韩国电影久久 | 午夜AV国产欧美亚洲高清在线 | 久久66热在线视频精品 | 草莓国产视频免费观看 | 91天堂国产在线 在线播放 | 伊人久久大香网 | 亚洲色图在线观看视频 | 强奷漂亮女老板在线播放 | 美女大鸡鸡 | 99在线观看 | 一个人色导航 | 亚洲AV久久婷婷蜜臀无码不卡 | 亚洲AVAV天堂AV在线网爱情 | 24小时日本高清免费看 | 国产亚洲视频在线 | 饥渴难耐的浪荡艳妇在线观看 | 日韩精品亚洲专区在线影院 | 俄罗斯老妇女BBXX | 国外色幼网 | 日韩欧美一区二区三区在线 | 日本漫画母亲口工子全彩 | 久久www成人看片 | 中文字幕乱码亚洲无线三区 | 久久丫线这里只精品 | 亚洲AV精品无码成人 | 国产人在线成免费视频 | xxxx18动漫 | 女bbbbxxx孕妇 | 国产99视频精品免费播放 | 日韩欧美 亚洲视频 | 日韩a在线看免费观看视频 日韩a视频在线观看 | 兰桂坊人成社区亚洲精品 | 国产精品久久精品视 | 久久99re2在线视频精品 | 东京热百度影音 | 亚洲 天堂 国产在线播放 | 国产成人片视频一区二区青青 | 亚洲视频在线观看地址 | 最新日本免费一区 | 超碰在线 视频 | 成人永久免费视频 |