javascript下有关dom以及xml节点访问兼容问题
最近整理浏览器兼容的问题,搞的实在头大,在前人的帮助之下,还是有点进展,下面帖一些代码,我想会比较有用
代码如下:
var isIE = ????; // 全局变量,判断是否ie,自完善 // new dom 方法 function parseXML(st){ if (isIE){ var result = new ActiveXObject( "microsoft.XMLDOM" ); result.loadXML(st); } else { var parser = new DOMParser(); var result = parser.parseFromString(st, "text/xml" ); } return result; } if ( ! isIE){ var ex; // 定义节点xml属性 XMLDocument.prototype.__proto__.__defineGetter__( "xml" , function (){ try { return new XMLSerializer().serializeToString( this ); } catch (ex){ var d = document.createElement( "div" ); d.appendChild( this .cloneNode( true )); return d.innerHTML; } }); Element.prototype.__proto__.__defineGetter__( "xml" , function (){ try { return new XMLSerializer().serializeToString( this ); } catch (ex){ var d = document.createElement( "div" ); d.appendChild( this .cloneNode( true )); return d.innerHTML; } }); //定义节点text属性 XMLDocument.prototype.__proto__.__defineGetter__( "text" , function (){ return this .firstChild.textContent }); Element.prototype.__proto__.__defineGetter__( "text" , function (){ return this .textContent }); // 定义selectSingleNode、selectNodes 方法 XMLDocument.prototype.selectSingleNode = Element.prototype.selectSingleNode = function (xpath){ var x = this .selectNodes(xpath) if ( ! x || x.length < 1 ) return null ; return x[ 0 ]; } XMLDocument.prototype.selectNodes = Element.prototype.selectNodes = function (xpath){ var xpe = new XPathEvaluator(); var nsResolver = xpe.createNSResolver( this .ownerDocument == null ? this .documentElement : this .ownerDocument.documentElement); var result = xpe.evaluate(xpath, this , nsResolver, 0 , null ); var found = []; var res; while (res = result.iterateNext()) found.push(res); return found; } }
相关推荐
Yakamoz 2020-05-26
与卿画眉共浮生 2020-10-14
xiyang 2020-08-21
baijinswpu 2020-07-29
leonranri 2020-07-26
zhongliwen 2020-07-05
麋鹿麋鹿迷了路 2020-07-05
zengyu00 2020-07-05
XGQ 2020-07-04
CoderBoy 2020-06-28
whbing 2020-06-28
绝望的乐园 2020-06-27
wellfly 2020-06-26
菇星獨行 2020-06-25
草原孤狼 2020-06-25
坚持着执着 2020-06-16
wcqwcq 2020-06-14
yuanye0 2020-06-14
zhongliwen 2020-06-13