javascript firefox兼容ie的dom方法腳本
更新時(shí)間:2008年05月18日 21:50:48 作者:
js firefox下兼容ie的dom的實(shí)現(xiàn)方法小結(jié)。利用我們?yōu)榱思嫒荻酁g覽器寫(xiě)出更好的效果代碼
if(!document.all){
//zzcv的ff ie兼容腳本
/*腳本沒(méi)有解決的問(wèn)題及處理:
2.IE下,可以使用()或[]獲取集合類(lèi)對(duì)象;Firefox下,只能使用[]獲取集合類(lèi)對(duì)象.
解決方法:統(tǒng)一使用[]獲取集合類(lèi)對(duì)象.
3.IE下,可以使用獲取常規(guī)屬性的方法來(lái)獲取自定義屬性,也可以使用getAttribute()獲取自定義屬性;Firefox下,只能使用getAttribute()獲取自定義屬性.
解決方法:統(tǒng)一通過(guò)getAttribute()獲取自定義屬性.
4.IE下,HTML對(duì)象的ID可以作為document的下屬對(duì)象變量名直接使用;Firefox下則不能.
5.Firefox下,可以使用與HTML對(duì)象ID相同的變量名;IE下則不能。
解決方法:使用document.getElementById("idName")代替document.idName.最好不要取HTML對(duì)象ID相同的變量名,以減少錯(cuò)誤;在聲明變量時(shí),一律加上var,以避免歧義.
6.IE下input.type屬性為只讀;但是Firefox下input.type屬性為讀寫(xiě).
8.IE下,可以通過(guò)showModalDialog和showModelessDialog打開(kāi)模態(tài)和非模態(tài)窗口;Firefox下則不能
9.Firefox的body在body標(biāo)簽沒(méi)有被瀏覽器完全讀入之前就存在;而IE的body則必須在body標(biāo)簽被瀏覽器完全讀入之后才存在
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;});
}
//節(jié)點(diǎn)操作兼容
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;});
}
//zzcv的ff ie兼容腳本
/*腳本沒(méi)有解決的問(wèn)題及處理:
2.IE下,可以使用()或[]獲取集合類(lèi)對(duì)象;Firefox下,只能使用[]獲取集合類(lèi)對(duì)象.
解決方法:統(tǒng)一使用[]獲取集合類(lèi)對(duì)象.
3.IE下,可以使用獲取常規(guī)屬性的方法來(lái)獲取自定義屬性,也可以使用getAttribute()獲取自定義屬性;Firefox下,只能使用getAttribute()獲取自定義屬性.
解決方法:統(tǒng)一通過(guò)getAttribute()獲取自定義屬性.
4.IE下,HTML對(duì)象的ID可以作為document的下屬對(duì)象變量名直接使用;Firefox下則不能.
5.Firefox下,可以使用與HTML對(duì)象ID相同的變量名;IE下則不能。
解決方法:使用document.getElementById("idName")代替document.idName.最好不要取HTML對(duì)象ID相同的變量名,以減少錯(cuò)誤;在聲明變量時(shí),一律加上var,以避免歧義.
6.IE下input.type屬性為只讀;但是Firefox下input.type屬性為讀寫(xiě).
8.IE下,可以通過(guò)showModalDialog和showModelessDialog打開(kāi)模態(tài)和非模態(tài)窗口;Firefox下則不能
9.Firefox的body在body標(biāo)簽沒(méi)有被瀏覽器完全讀入之前就存在;而IE的body則必須在body標(biāo)簽被瀏覽器完全讀入之后才存在
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;});
}
//節(jié)點(diǎn)操作兼容
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;});
}
您可能感興趣的文章:
- javascript 獲取HTML DOM父、子、臨近節(jié)點(diǎn)
- JavaScript 節(jié)點(diǎn)操作 以及DOMDocument屬性和方法
- javascript獲取dom的下一個(gè)節(jié)點(diǎn)方法
- JavaScript獲取DOM元素的11種方法總結(jié)
- JavaScript DOM節(jié)點(diǎn)操作方法總結(jié)
- javascript dom操作之cloneNode文本節(jié)點(diǎn)克隆使用技巧
- JavaScript與DOM組合動(dòng)態(tài)創(chuàng)建表格實(shí)例
- JavaScript實(shí)現(xiàn)獲取dom中class的方法
- JavaScript中對(duì)DOM節(jié)點(diǎn)的訪(fǎng)問(wèn)、創(chuàng)建、修改、刪除
- javascript DOM 操作基礎(chǔ)知識(shí)小結(jié)
- 淺談Javascript中的12種DOM節(jié)點(diǎn)類(lèi)型
- js中script的上下放置區(qū)別,Dom的增刪改創(chuàng)建操作實(shí)例分析
相關(guān)文章
JS如何實(shí)現(xiàn)form表單登錄驗(yàn)證并使用MD5加密詳解
表單驗(yàn)證為終端用戶(hù)檢測(cè)無(wú)效的數(shù)據(jù)并標(biāo)記這些錯(cuò)誤,是一種用戶(hù)體驗(yàn)的優(yōu)化,下面這篇文章主要給大家介紹了關(guān)于JS如何實(shí)現(xiàn)form表單登錄驗(yàn)證并使用MD5加密的相關(guān)資料,需要的朋友可以參考下2023-06-06
一個(gè)JavaScript函數(shù)把URL參數(shù)解析成Json對(duì)象
一個(gè)JavaScript函數(shù)parseQueryString,它的用途是把URL參數(shù)解析為一個(gè)對(duì)象,很實(shí)用,大家可以看看2014-09-09
微信小程序?qū)崿F(xiàn)商城倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)商城倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
小程序開(kāi)發(fā)實(shí)現(xiàn)access_token統(tǒng)一管理
本文主要介紹了小程序開(kāi)發(fā)實(shí)現(xiàn)access_token統(tǒng)一管理,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
JavaScript獲取數(shù)組最后一個(gè)元素的3種方法以及性能
在開(kāi)發(fā)過(guò)程中,我們常常需要得到j(luò)s數(shù)組的最后一個(gè)數(shù)組元素,下面這篇文章主要給大家介紹了關(guān)于JavaScript獲取數(shù)組最后一個(gè)元素的3種方法以及性能,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
原生js模擬淘寶購(gòu)物車(chē)項(xiàng)目實(shí)戰(zhàn)
這篇文章主要向大家推薦了一個(gè)原生js模擬淘寶購(gòu)物車(chē)項(xiàng)目實(shí)戰(zhàn),包括商品的單選、全選、刪除、修改數(shù)量、價(jià)格計(jì)算、數(shù)目計(jì)算、預(yù)覽等功能的實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2015-11-11
一文看懂如何簡(jiǎn)單實(shí)現(xiàn)節(jié)流函數(shù)和防抖函數(shù)
這篇文章主要給大家介紹了如何通過(guò)一文看懂簡(jiǎn)單實(shí)現(xiàn)節(jié)流函數(shù)和防抖函數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
yii form 表單提交之前JS在提交按鈕的驗(yàn)證方法
很多時(shí)候,需要對(duì)Yii表單model中的對(duì)象設(shè)置的rules進(jìn)行判斷,但是有的時(shí)候可能需要在提交之前就在客戶(hù)端進(jìn)行驗(yàn)證。怎么處理呢?接下來(lái)通過(guò)本文給大家分享yii form 表單提交之前JS在提交按鈕的驗(yàn)證方法,需要的的朋友參考下2017-03-03

