javascript下有关dom以及xml节点访问兼容问题
人气:0
最近整理浏览器兼容的问题,搞的实在头大,在前人的帮助之下,还是有点进展,下面帖一些代码,我想会比较有用
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;
}
}
加载全部内容