Home > Web Front-end > JS Tutorial > Add IE methods and attributes to moz-firefox_javascript skills

Add IE methods and attributes to moz-firefox_javascript skills

WBOY
Release: 2016-05-16 19:15:07
Original
1055 people have browsed it

I saw the JS written by Xinyun on exchanging selects in IECN. Because it uses methods such as removeNode and swapNode, it is invalid under Firefox. I just Googled it and found that you can modify the properties and methods that are only valid under IE through custom prototypes.

Original reference: http://www.phpx.com/happy/top97619.html

The modification plan is as follows:


<script> <BR><!-- <BR>if(window.Event){// 修正Event的DOM <BR> /* <BR> IE5 MacIE5 Mozilla Konqueror2.2 Opera5 <BR> event yes yes yes yes yes <BR> event.returnValue yes yes no no no <BR> event.cancelBubble yes yes no no no <BR> event.srcElement yes yes no no no <BR> event.fromElement yes yes no no no <br><br> */ <BR> Event.prototype.__defineSetter__("returnValue",function(b){// <BR> if(!b)this.preventDefault(); <BR> return b; <BR> }); <BR> Event.prototype.__defineSetter__("cancelBubble",function(b){// 设置或者检索当前事件句柄的层次冒泡 <BR> if(b)this.stopPropagation(); <BR> return b; <BR> }); <BR> Event.prototype.__defineGetter__("srcElement",function(){ <BR> var node=this.target; <BR> while(node.nodeType!=1)node=node.parentNode; <BR> return node; <BR> }); <BR> Event.prototype.__defineGetter__("fromElement",function(){// 返回鼠标移出的源节点 <BR> var node; <BR> if(this.type=="mouseover") <BR> node=this.relatedTarget; <BR> else if(this.type=="mouseout") <BR> node=this.target; <BR> if(!node)return; <BR> while(node.nodeType!=1)node=node.parentNode; <BR> return node; <BR> }); <BR> Event.prototype.__defineGetter__("toElement",function(){// 返回鼠标移入的源节点 <BR> var node; <BR> if(this.type=="mouseout") <BR> node=this.relatedTarget; <BR> else if(this.type=="mouseover") <BR> node=this.target; <BR> if(!node)return; <BR> while(node.nodeType!=1)node=node.parentNode; <BR> return node; <BR> }); <BR> Event.prototype.__defineGetter__("offsetX",function(){ <BR> return this.layerX; <BR> }); <BR> Event.prototype.__defineGetter__("offsetY",function(){ <BR> return this.layerY; <BR> }); <BR> } <BR>if(window.Document){// 修正Document的DOM <BR> /* <BR> IE5 MacIE5 Mozilla Konqueror2.2 Opera5 <BR> document.documentElement yes yes yes yes no <BR> document.activeElement yes null no no no <br><br> */ <BR> } <BR>if(window.Node){// 修正Node的DOM <BR> /* <BR> IE5 MacIE5 Mozilla Konqueror2.2 Opera5 <BR> Node.contains yes yes no no yes <BR> Node.replaceNode yes no no no no <BR> Node.removeNode yes no no no no <BR> Node.children yes yes no no no <BR> Node.hasChildNodes yes yes yes yes no <BR> Node.childNodes yes yes yes yes no <BR> Node.swapNode yes no no no no <BR> Node.currentStyle yes yes no no no <br><br> */ <BR> Node.prototype.replaceNode=function(Node){// 替换指定节点 <BR> this.parentNode.replaceChild(Node,this); <BR> } <BR> Node.prototype.removeNode=function(removeChildren){// 删除指定节点 <BR> if(removeChildren) <BR> return this.parentNode.removeChild(this); <BR> else{ <BR> var range=document.createRange(); <BR> range.selectNodeContents(this); <BR> return this.parentNode.replaceChild(range.extractContents(),this); <BR> } <BR> } <BR> Node.prototype.swapNode=function(Node){// 交换节点 <BR> var nextSibling=this.nextSibling; <BR> var parentNode=this.parentNode; <BR> node.parentNode.replaceChild(this,Node); <BR> parentNode.insertBefore(node,nextSibling); <BR> } <BR> } <BR>if(window.HTMLElement){ <BR> HTMLElement.prototype.__defineGetter__("all",function(){ <BR> var a=this.getElementsByTagName("*"); <BR> var node=this; <BR> a.tags=function(sTagName){ <BR> return node.getElementsByTagName(sTagName); <BR> } <BR> return a; <BR> }); <BR> HTMLElement.prototype.__defineGetter__("parentElement",function(){ <BR> if(this.parentNode==this.ownerDocument)return null; <BR> return this.parentNode; <BR> }); <BR> HTMLElement.prototype.__defineGetter__("children",function(){ <BR> var tmp=[]; <BR> var j=0; <BR> var n; <BR> for(var i=0;i<this.childNodes.length;i++){ <BR> n=this.childNodes[i]; <BR> if(n.nodeType==1){ <BR> tmp[j++]=n; <BR> if(n.name){ <BR> if(!tmp[n.name]) <BR> tmp[n.name]=[]; <BR> tmp[n.name][tmp[n.name].length]=n; <BR> } <BR> if(n.id) <BR> tmp[n.id]=n; <BR> } <BR> } <BR> return tmp; <BR> }); <BR> HTMLElement.prototype.__defineGetter__("currentStyle", function(){ <BR> return this.ownerDocument.defaultView.getComputedStyle(this,null); <BR> }); <BR> HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){ <BR> var r=this.ownerDocument.createRange(); <BR> r.setStartBefore(this); <BR> var df=r.createContextualFragment(sHTML); <BR> this.parentNode.replaceChild(df,this); <BR> return sHTML; <BR> }); <BR> HTMLElement.prototype.__defineGetter__("outerHTML",function(){ <BR> var attr; <BR> var attrs=this.attributes; <BR> var str="<"+this.tagName; <BR> for(var i=0;i<attrs.length;i++){ <BR> attr=attrs[i]; <BR> if(attr.specified) <BR> str+=" "+attr.name+'="'+attr.value+'"'; <BR> } <BR> if(!this.canHaveChildren) <BR> return str+">"; <BR> return str+">"+this.innerHTML+"</"+this.tagName+">"; <BR> }); <BR> HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){ <BR> switch(this.tagName.toLowerCase()){ <BR> case "area": <BR> case "base": <BR> case "basefont": <BR> case "col": <BR> case "frame": <BR> case "hr": <BR> case "img": <BR> case "br": <BR> case "input": <BR> case "isindex": <BR> case "link": <BR> case "meta": <BR> case "param": <BR> return false; <BR> } <BR> return true; <BR> }); <br><br> HTMLElement.prototype.__defineSetter__("innerText",function(sText){ <BR> var parsedText=document.createTextNode(sText); <BR> this.innerHTML=parsedText; <BR> return parsedText; <BR> }); <BR> HTMLElement.prototype.__defineGetter__("innerText",function(){ <BR> var r=this.ownerDocument.createRange(); <BR> r.selectNodeContents(this); <BR> return r.toString(); <BR> }); <BR> HTMLElement.prototype.__defineSetter__("outerText",function(sText){ <BR> var parsedText=document.createTextNode(sText); <BR> this.outerHTML=parsedText; <BR> return parsedText; <BR> }); <BR> HTMLElement.prototype.__defineGetter__("outerText",function(){ <BR> var r=this.ownerDocument.createRange(); <BR> r.selectNodeContents(this); <BR> return r.toString(); <BR> }); <BR> HTMLElement.prototype.attachEvent=function(sType,fHandler){ <BR> var shortTypeName=sType.replace(/on/,""); <BR> fHandler._ieEmuEventHandler=function(e){ <BR> window.event=e; <BR> return fHandler(); <BR> } <BR> this.addEventListener(shortTypeName,fHandler._ieEmuEventHandler,false); <BR> } <BR> HTMLElement.prototype.detachEvent=function(sType,fHandler){ <BR> var shortTypeName=sType.replace(/on/,""); <BR> if(typeof(fHandler._ieEmuEventHandler)=="function") <BR> this.removeEventListener(shortTypeName,fHandler._ieEmuEventHandler,false); <BR> else <BR> this.removeEventListener(shortTypeName,fHandler,true); <BR> } <BR> HTMLElement.prototype.contains=function(Node){// 是否包含某节点 <BR> do if(Node==this)return true; <BR> while(Node=Node.parentNode); <BR> return false; <BR> } <BR> HTMLElement.prototype.insertAdjacentElement=function(where,parsedNode){ <BR> switch(where){ <BR> case "beforeBegin": <BR> this.parentNode.insertBefore(parsedNode,this); <BR> break; <BR> case "afterBegin": <BR> this.insertBefore(parsedNode,this.firstChild); <BR> break; <BR> case "beforeEnd": <BR> this.appendChild(parsedNode); <BR> break; <BR> case "afterEnd": <BR> if(this.nextSibling) <BR> this.parentNode.insertBefore(parsedNode,this.nextSibling); <BR> else <BR> this.parentNode.appendChild(parsedNode); <BR> break; <BR> } <BR> } <BR> HTMLElement.prototype.insertAdjacentHTML=function(where,htmlStr){ <BR> var r=this.ownerDocument.createRange(); <BR> r.setStartBefore(this); <BR> var parsedHTML=r.createContextualFragment(htmlStr); <BR> this.insertAdjacentElement(where,parsedHTML); <BR> } <BR> HTMLElement.prototype.insertAdjacentText=function(where,txtStr){ <BR> var parsedText=document.createTextNode(txtStr); <BR> this.insertAdjacentElement(where,parsedText); <BR> } <BR> HTMLElement.prototype.attachEvent=function(sType,fHandler){ <BR> var shortTypeName=sType.replace(/on/,""); <BR> fHandler._ieEmuEventHandler=function(e){ <BR> window.event=e; <BR> return fHandler(); <BR> } <BR> this.addEventListener(shortTypeName,fHandler._ieEmuEventHandler,false); <BR> } <BR> HTMLElement.prototype.detachEvent=function(sType,fHandler){ <BR> var shortTypeName=sType.replace(/on/,""); <BR> if(typeof(fHandler._ieEmuEventHandler)=="function") <BR> this.removeEventListener(shortTypeName,fHandler._ieEmuEventHandler,false); <BR> else <BR> this.removeEventListener(shortTypeName,fHandler,true); <BR> } <BR> } <BR>//--> <BR></script>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template