javascript firefox兼容ie的dom方法脚本

时间:2021-05-28

if(!document.all){
//zzcv的ffie兼容脚本

//文档兼容
HTMLDocument.prototype.__defineGetter__("all",function(){
returnthis.getElementsByName("*");});

HTMLFormElement.constructor.prototype.item=function(s){
returnthis.elements[s];};

HTMLCollection.prototype.item=function(s){
returnthis[s];};

//事件兼容
window.constructor.prototype.__defineGetter__("event",function(){
for(varo=arguments.callee.caller,e=null;o!=null;o=o.caller){
e=o.arguments[0];
if(e&&(einstanceofEvent))
returne;}
returnnull;});

window.constructor.prototype.attachEvent=HTMLDocument.prototype.attachEvent=HTMLElement.prototype.attachEvent=function(e,f){
this.addEventListener(e.replace(/^on/i,""),f,false);};

window.constructor.prototype.detachEvent=HTMLDocument.prototype.detachEvent=HTMLElement.prototype.detachEvent=function(e,f){
this.removeEventListener(e.replace(/^on/i,""),f,false);};


with(window.Event.constructor.prototype){
__defineGetter__("srcElement",function(){
returnthis.target;});

__defineSetter__("returnValue",function(b){
if(!b)this.preventDefault();});

__defineSetter__("cancelBubble",function(b){
if(b)this.stopPropagation();});

__defineGetter__("fromElement",function(){
varo=(this.type=="mouseover"&&this.relatedTarget)||(this.type=="mouseout"&&this.target)||null;
if(o)
while(o.nodeType!=1)
o=o.parentNode;
returno;});

__defineGetter__("toElement",function(){
varo=(this.type=="mouseover"&&this.target)||(this.type=="mouseout"&&this.relatedTarget)||null;
if(o)
while(o.nodeType!=1)
o=o.parentNode;
returno;});

__defineGetter__("x",function(){
returnthis.pageX;});

__defineGetter__("y",function(){
returnthis.pageY;});

__defineGetter__("offsetX",function(){
returnthis.layerX;});

__defineGetter__("offsetY",function(){
returnthis.layerY;});
}
//节点操作兼容
with(window.Node.prototype){
replaceNode=function(o){
this.parentNode.replaceChild(o,this);}

removeNode=function(b){
if(b)
returnthis.parentNode.removeChild(this);
varrange=document.createRange();
range.selectNodeContents(this);
returnthis.parentNode.replaceChild(range.extractContents(),this);}

swapNode=function(o){
returnthis.parentNode.replaceChild(o.parentNode.replaceChild(this,o),this);}

contains=function(o){
returno?((o==this)?true:arguments.callee(o.parentNode)):false;}
}
//HTML元素兼容
with(window.HTMLElement.prototype){
__defineGetter__("parentElement",function(){
return(this.parentNode==this.ownerDocument)?null:this.parentNode;});

__defineGetter__("children",function(){
varc=[];
for(vari=0,cs=this.childNodes;i<cs.length;i++){
if(cs[i].nodeType==1)
c.push(cs[i]);}
returnc;});

__defineGetter__("canHaveChildren",function(){
return!/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/i.test(this.tagName);});

__defineSetter__("outerHTML",function(s){
varr=this.ownerDocument.createRange();
r.setStartBefore(this);
voidthis.parentNode.replaceChild(r.createContextualFragment(s),this);
returns;});
__defineGetter__("outerHTML",function(){
varas=this.attributes;
varstr="<"+this.tagName;
for(vari=0,al=as.length;i<al;i++){
if(as[i].specified)
str+=""+as[i].name+"=""+as[i].value+""";}
returnthis.canHaveChildren?str+">":str+">"+this.innerHTML+"</"+this.tagName+">";});

__defineSetter__("innerText",function(s){
returnthis.innerHTML=document.createTextNode(s);});
__defineGetter__("innerText",function(){
varr=this.ownerDocument.createRange();
r.selectNodeContents(this);
returnr.toString();});

__defineSetter__("outerText",function(s){
voidthis.parentNode.replaceChild(document.createTextNode(s),this);
returns});
__defineGetter__("outerText",function(){
varr=this.ownerDocument.createRange();
r.selectNodeContents(this);
returnr.toString();});

insertAdjacentElement=function(s,o){
return(s=="beforeBegin"&&this.parentNode.insertBefore(o,this))||(s=="afterBegin"&&this.insertBefore(o,this.firstChild))||(s=="beforeEnd"&&this.appendChild(o))||(s=="afterEnd"&&((this.nextSibling)&&this.parentNode.insertBefore(o,this.nextSibling)||this.parentNode.appendChild(o)))||null;}

insertAdjacentHTML=function(s,h){
varr=this.ownerDocument.createRange();
r.setStartBefore(this);
this.insertAdjacentElement(s,r.createContextualFragment(h));}

insertAdjacentText=function(s,t){
this.insertAdjacentElement(s,document.createTextNode(t));}
}
//XMLDOM兼容
window.ActiveXObject=function(s){
switch(s){
case"XMLDom":
document.implementation.createDocument.call(this,"text/xml","",null);
//domDoc=document.implementation.createDocument("text/xml","",null);
break;
}
}

XMLDocument.prototype.LoadXML=function(s){
for(vari=0,cs=this.childNodes,cl=childNodes.length;i<cl;i++)
this.removeChild(cs[i]);
this.appendChild(this.importNode((newDOMParser()).parseFromString(s,"text/xml").documentElement,true));}

XMLDocument.prototype.selectSingleNode=Element.prototype.selectSingleNode=function(s){
returnthis.selectNodes(s)[0];}
XMLDocument.prototype.selectNodes=Element.prototype.selectNodes=function(s){
varrt=[];
for(vari=0,rs=this.evaluate(s,this,this.createNSResolver(this.ownerDocument==null?this.documentElement:this.ownerDocument.documentElement),XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),sl=rs.snapshotLength;i<sl;i++)
rt.push(rs.snapshotItem(i));
returnrt;}

XMLDocument.prototype.__proto__.__defineGetter__("xml",function(){
try{
returnnewXMLSerializer().serializeToString(this);}
catch(e){
returndocument.createElement("div").appendChild(this.cloneNode(true)).innerHTML;}});
Element.prototype.__proto__.__defineGetter__("xml",function(){
try{
returnnewXMLSerializer().serializeToString(this);}
catch(e){
returndocument.createElement("div").appendChild(this.cloneNode(true)).innerHTML;}});

XMLDocument.prototype.__proto__.__defineGetter__("text",function(){
returnthis.firstChild.textContent;});

Element.prototype.__proto__.__defineGetter__("text",function(){
returnthis.textContent;});
Element.prototype.__proto__.__defineSetter__("text",function(s){
returnthis.textContent=s;});

}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章