贴一个在Mozilla中常用的Javascript代码_Javascript教程-查字典教程网
贴一个在Mozilla中常用的Javascript代码
贴一个在Mozilla中常用的Javascript代码
发布时间:2016-12-30 来源:查字典编辑
摘要:Mozilla中独有的读写器(defineGetter、defineSetter)以及可以给Element,Event等加上prototyp...

Mozilla中独有的读写器(defineGetter、defineSetter)以及可以给Element,Event等加上prototype原型,使得在IE里用的方法同样在Mozilla中可以适用,下面贴出常用的一些代码

例如

obj.insertAdjacentHTML,currentStyle,obj.attachEvent,obj.detachEvent等等。

版权属于ErikArvidsson,webfx

复制代码 代码如下:if(Browser.isMozilla){//setupieenvironmentforMoz

extendEventObject();

emulateAttachEvent();

emulateEventHandlers(["click","dblclick","mouseover","mouseout",

"mousedown","mouseup","mousemove",

"keydown","keypress","keyup"]);

emulateCurrentStyle();

/*emulateDocumentAll();

emulateElement()

*/

//Itisbettertouseaconstantforevent.button

Event.LEFT=0;

Event.MIDDLE=1;

Event.RIGHT=2;

}

else{

Event={};

//IEisreturningwrongbuttonnumber

Event.LEFT=1;

Event.MIDDLE=4;

Event.RIGHT=2;

}

/*

*ExtendstheeventobjectwithsrcElement,cancelBubble,returnValue,

*fromElementandtoElement

*/

functionextendEventObject(){

Event.prototype.__defineSetter__("returnValue",function(b){

if(!b)this.preventDefault();

returnb;

});

Event.prototype.__defineSetter__("cancelBubble",function(b){

if(b)this.stopPropagation();

returnb;

});

Event.prototype.__defineGetter__("srcElement",function(){

varnode=this.target;

while(node.nodeType!=1)node=node.parentNode;

returnnode;

});

Event.prototype.__defineGetter__("fromElement",function(){

varnode;

if(this.type=="mouseover")

node=this.relatedTarget;

elseif(this.type=="mouseout")

node=this.target;

if(!node)return;

while(node.nodeType!=1)node=node.parentNode;

returnnode;

});

Event.prototype.__defineGetter__("toElement",function(){

varnode;

if(this.type=="mouseout")

node=this.relatedTarget;

elseif(this.type=="mouseover")

node=this.target;

if(!node)return;

while(node.nodeType!=1)node=node.parentNode;

returnnode;

});

Event.prototype.__defineGetter__("offsetX",function(){

returnthis.layerX;

});

Event.prototype.__defineGetter__("offsetY",function(){

returnthis.layerY;

});

}

/*

*Emulateselement.attachEventaswellasdetachEvent

*/

functionemulateAttachEvent(){

HTMLDocument.prototype.attachEvent=

HTMLElement.prototype.attachEvent=function(sType,fHandler){

varshortTypeName=sType.replace(/on/,"");

fHandler._ieEmuEventHandler=function(e){

window.event=e;

returnfHandler();

};

this.addEventListener(shortTypeName,fHandler._ieEmuEventHandler,false);

};

HTMLDocument.prototype.detachEvent=

HTMLElement.prototype.detachEvent=function(sType,fHandler){

varshortTypeName=sType.replace(/on/,"");

if(typeoffHandler._ieEmuEventHandler=="function")

this.removeEventListener(shortTypeName,fHandler._ieEmuEventHandler,false);

else

this.removeEventListener(shortTypeName,fHandler,true);

};

}

/*

*Thisfunctionbindstheeventobjectpassedalonginan

*eventtowindow.event

*/

functionemulateEventHandlers(eventNames){

for(vari=0;i<eventNames.length;i++){

document.addEventListener(eventNames[i],function(e){

window.event=e;

},true);//usingcapture

}

}

/*

*Simpleemulationofdocument.all

*thisoneisfarfromcomplete.Becautious

*/

functionemulateAllModel(){

varallGetter=function(){

vara=this.getElementsByTagName("*");

varnode=this;

a.tags=function(sTagName){

returnnode.getElementsByTagName(sTagName);

};

returna;

};

HTMLDocument.prototype.__defineGetter__("all",allGetter);

HTMLElement.prototype.__defineGetter__("all",allGetter);

}

functionextendElementModel(){

HTMLElement.prototype.__defineGetter__("parentElement",function(){

if(this.parentNode==this.ownerDocument)returnnull;

returnthis.parentNode;

});

HTMLElement.prototype.__defineGetter__("children",function(){

vartmp=[];

varj=0;

varn;

for(vari=0;i<this.childNodes.length;i++){

n=this.childNodes[i];

if(n.nodeType==1){

tmp[j++]=n;

if(n.name){//namedchildren

if(!tmp[n.name])

tmp[n.name]=[];

tmp[n.name][tmp[n.name].length]=n;

}

if(n.id)//childwithid

tmp[n.id]=n

}

}

returntmp;

});

HTMLElement.prototype.contains=function(oEl){

if(oEl==this)returntrue;

if(oEl==null)returnfalse;

returnthis.contains(oEl.parentNode);

};

}

functionemulateCurrentStyle(){

HTMLElement.prototype.__defineGetter__("currentStyle",function(){

returnthis.ownerDocument.defaultView.getComputedStyle(this,null);

/*

varcs={};

varel=this;

for(vari=0;i<properties.length;i++){

cs.__defineGetter__(properties[i],encapsulateObjects(el,properties[i]));

}

returncs;

*/

});

}

functionemulateHTMLModel(){

//Thisfunctionisusedtogenerateahtmlstringforthetextproperties/methods

//Itreplaces'n'with"<BR">aswellasfixesconsecutivewhitespaces

//Italsorepalacessomespecialcharacters

functionconvertTextToHTML(s){

s=s.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/n/g,"<BR>");

while(/ss/.test(s))

s=s.replace(/ss/,"");

returns.replace(/s/g,"");

}

HTMLElement.prototype.insertAdjacentHTML=function(sWhere,sHTML){

vardf;//:DocumentFragment

varr=this.ownerDocument.createRange();

switch(String(sWhere).toLowerCase()){

case"beforebegin":

r.setStartBefore(this);

df=r.createContextualFragment(sHTML);

this.parentNode.insertBefore(df,this);

break;

case"afterbegin":

r.selectNodeContents(this);

r.collapse(true);

df=r.createContextualFragment(sHTML);

this.insertBefore(df,this.firstChild);

break;

case"beforeend":

r.selectNodeContents(this);

r.collapse(false);

df=r.createContextualFragment(sHTML);

this.appendChild(df);

break;

case"afterend":

r.setStartAfter(this);

df=r.createContextualFragment(sHTML);

this.parentNode.insertBefore(df,this.nextSibling);

break;

}

};

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__("canHaveChildren",function(){

switch(this.tagName){

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.__defineGetter__("outerHTML",function(){

varattr,attrs=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.__defineSetter__("innerText",function(sText){

this.innerHTML=convertTextToHTML(sText);

returnsText;

});

vartmpGet;

HTMLElement.prototype.__defineGetter__("innerText",tmpGet=function(){

varr=this.ownerDocument.createRange();

r.selectNodeContents(this);

returnr.toString();

});

HTMLElement.prototype.__defineSetter__("outerText",function(sText){

this.outerHTML=convertTextToHTML(sText);

returnsText;

});

HTMLElement.prototype.__defineGetter__("outerText",tmpGet);

HTMLElement.prototype.insertAdjacentText=function(sWhere,sText){

this.insertAdjacentHTML(sWhere,convertTextToHTML(sText));

};

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类