给moz-firefox下添加IE方法和属性

时间:2021-05-18

在IECN看到心云写的关于互换select的JS,因里面用到removeNode和swapNode等方法,导致在Firefox下无效。刚刚Google了下,发现可以通过自定义原型来修正只在IE下有效的属性与方法。

原文参考:http://putedStyle(this,null);
});
HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){
varr=this.ownerDocument.createRange();
r.setStartBefore(this);
vardf=r.createContextualFragment(sHTML);
this.parentNode.replaceChild(df,this);
returnsHTML;
});
HTMLElement.prototype.__defineGetter__("outerHTML",function(){
varattr;
varattrs=this.attributes;
varstr="<"+this.tagName;
for(vari=0;i<attrs.length;i++){
attr=attrs[i];
if(attr.specified)
str+=""+attr.name+'="'+attr.value+'"';
}
if(!this.canHaveChildren)
returnstr+">";
returnstr+">"+this.innerHTML+"</"+this.tagName+">";
});
HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
switch(this.tagName.toLowerCase()){
case"area":
case"base":
case"basefont":
case"col":
case"frame":
case"hr":
case"img":
case"br":
case"input":
case"isindex":
case"link":
case"meta":
case"param":
returnfalse;
}
returntrue;
});

HTMLElement.prototype.__defineSetter__("innerText",function(sText){
varparsedText=document.createTextNode(sText);
this.innerHTML=parsedText;
returnparsedText;
});
HTMLElement.prototype.__defineGetter__("innerText",function(){
varr=this.ownerDocument.createRange();
r.selectNodeContents(this);
returnr.toString();
});
HTMLElement.prototype.__defineSetter__("outerText",function(sText){
varparsedText=document.createTextNode(sText);
this.outerHTML=parsedText;
returnparsedText;
});
HTMLElement.prototype.__defineGetter__("outerText",function(){
varr=this.ownerDocument.createRange();
r.selectNodeContents(this);
returnr.toString();
});
HTMLElement.prototype.attachEvent=function(sType,fHandler){
varshortTypeName=sType.replace(/on/,"");
fHandler._ieEmuEventHandler=function(e){
window.event=e;
returnfHandler();
}
this.addEventListener(shortTypeName,fHandler._ieEmuEventHandler,false);
}
HTMLElement.prototype.detachEvent=function(sType,fHandler){
varshortTypeName=sType.replace(/on/,"");
if(typeof(fHandler._ieEmuEventHandler)=="function")
this.removeEventListener(shortTypeName,fHandler._ieEmuEventHandler,false);
else
this.removeEventListener(shortTypeName,fHandler,true);
}
HTMLElement.prototype.contains=function(Node){//是否包含某节点
doif(Node==this)returntrue;
while(Node=Node.parentNode);
returnfalse;
}
HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){
switch(where){
case"beforeBegin":
this.parentNode.insertBefore(parsedNode,this);
break;
case"afterBegin":
this.insertBefore(parsedNode,this.firstChild);
break;
case"beforeEnd":
this.appendChild(parsedNode);
break;
case"afterEnd":
if(this.nextSibling)
this.parentNode.insertBefore(parsedNode,this.nextSibling);
else
this.parentNode.appendChild(parsedNode);
break;
}
}
HTMLElement.prototype.insertAdjacentHTML=function(where,htmlStr){
varr=this.ownerDocument.createRange();
r.setStartBefore(this);
varparsedHTML=r.createContextualFragment(htmlStr);
this.insertAdjacentElement(where,parsedHTML);
}
HTMLElement.prototype.insertAdjacentText=function(where,txtStr){
varparsedText=document.createTextNode(txtStr);
this.insertAdjacentElement(where,parsedText);
}
HTMLElement.prototype.attachEvent=function(sType,fHandler){
varshortTypeName=sType.replace(/on/,"");
fHandler._ieEmuEventHandler=function(e){
window.event=e;
returnfHandler();
}
this.addEventListener(shortTypeName,fHandler._ieEmuEventHandler,false);
}
HTMLElement.prototype.detachEvent=function(sType,fHandler){
varshortTypeName=sType.replace(/on/,"");
if(typeof(fHandler._ieEmuEventHandler)=="function")
this.removeEventListener(shortTypeName,fHandler._ieEmuEventHandler,false);
else
this.removeEventListener(shortTypeName,fHandler,true);
}
}
//-->
</script>

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

相关文章