javascript firefox相容ie的dom方法指令碼

來源:互聯網
上載者:User

if(!document.all){
//zzcv的ff ie相容指令碼
/*指令碼沒有解決的問題及處理:

2.IE下,可以使用()或[]擷取集合類對象;Firefox下,只能使用[]擷取集合類對象.
解決方案:統一使用[]擷取集合類對象.
3.IE下,可以使用擷取常規屬性的方法來擷取自訂屬性,也可以使用getAttribute()擷取自訂屬性;Firefox下,只能使用getAttribute()擷取自訂屬性.
解決方案:統一通過getAttribute()擷取自訂屬性.
4.IE下,HTML對象的ID可以作為document的下屬物件變數名直接使用;Firefox下則不能.
5.Firefox下,可以使用與HTML對象ID相同的變數名;IE下則不能。
解決方案:使用document.getElementById("idName")代替document.idName.最好不要取HTML對象ID相同的變數名,以減少錯誤;在聲明變數時,一律加上var,以避免歧義.
6.IE下input.type屬性為唯讀;但是Firefox下input.type屬性為讀寫.
8.IE下,可以通過showModalDialog和showModelessDialog開啟模態和非模態視窗;Firefox下則不能
9.Firefox的body在body標籤沒有被瀏覽器完全讀入之前就存在;而IE的body則必須在body標籤被瀏覽器完全讀入之後才存在
10.
*/
//文檔相容
HTMLDocument.prototype.__defineGetter__("all",function(){
return this.getElementsByName("*");});

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

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

//事件相容
window.constructor.prototype.__defineGetter__("event",function(){
for(var o=arguments.callee.caller,e=null;o!=null;o=o.caller){
e=o.arguments[0];
if(e&&(e instanceof Event))
return e;}
return null;});

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(){
return this.target;});

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

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

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

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

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

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

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

__defineGetter__("offsetY",function(){
return this.layerY;});
}
//節點操作相容
with(window.Node.prototype){
replaceNode=function(o){
this.parentNode.replaceChild(o,this);}

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

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

contains=function(o){
return o?((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(){
var c=[];
for(var i=0,cs=this.childNodes;i<cs.length;i++){
if(cs[i].nodeType==1)
c.push(cs[i]);}
return c;});

__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){
var r=this.ownerDocument.createRange();
r.setStartBefore(this);
void this.parentNode.replaceChild(r.createContextualFragment(s),this);
return s;});
__defineGetter__("outerHTML",function(){
var as=this.attributes;
var str="<"+this.tagName;
for(var i=0,al=as.length;i<al;i++){
if(as[i].specified)
str+=" "+as[i].name+"=""+as[i].value+""";}
return this.canHaveChildren?str+">":str+">"+this.innerHTML+"</"+this.tagName+">";});

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

__defineSetter__("outerText",function(s){
void this.parentNode.replaceChild(document.createTextNode(s),this);
return s});
__defineGetter__("outerText",function(){
var r=this.ownerDocument.createRange();
r.selectNodeContents(this);
return r.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){
var r=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(var i=0,cs=this.childNodes,cl=childNodes.length;i<cl;i++)
this.removeChild(cs[i]);
this.appendChild(this.importNode((new DOMParser()).parseFromString(s,"text/xml").documentElement,true));}

XMLDocument.prototype.selectSingleNode=Element.prototype.selectSingleNode=function(s){
return this.selectNodes(s)[0];}
XMLDocument.prototype.selectNodes=Element.prototype.selectNodes=function(s){
var rt=[];
for(var i=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));
return rt;}

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

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

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

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.