javascript下有关dom以及xml节点访问兼容问题
时间:2021-09-26 10:17:06|栏目:JavaScript代码|点击: 次
最近整理浏览器兼容的问题,搞的实在头大,在前人的帮助之下,还是有点进展,下面帖一些代码,我想会比较有用
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;
}
}
复制代码 代码如下:
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;
}
}
上一篇:js 方法实现返回多个数据的代码
栏 目:JavaScript代码
下一篇:微信小程序 页面之间传参实例详解
本文标题:javascript下有关dom以及xml节点访问兼容问题
本文地址:http://www.codeinn.net/misctech/177453.html