手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >getElementsByTagName vs selectNodes效率 及兼容的selectNodes实现
getElementsByTagName vs selectNodes效率 及兼容的selectNodes实现
摘要:于是就测试了下:复制代码代码如下:varstringToDom=function(text){vardoc;if(window.Active...

于是就测试了下:

复制代码 代码如下:

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>

【getElementsByTagName vs selectNodes效率 及兼容的selectNodes实现】相关文章:

使用JavaScript刷新网页的方法

kindeditor编辑器点中图片滚动条往上顶的bug

JavaScript实现表格点击排序的方法

JQuery分屏指示器图片轮换效果实例

JavaScript框架是什么?怎样才能叫做框架?

JavaScript数据结构与算法之栈与队列

JS实现窗口加载时模拟鼠标移动的方法

理解javascript的caller,callee,call,apply概念

JavaScript实现文本框中默认显示背景图片在获得焦点后消失的方法

JavaScript获取当前运行脚本文件所在目录的方法

精品推荐
分类导航