>웹 프론트엔드 >JS 튜토리얼 >moz-firefox_javascript 기술에 IE 메소드 및 속성 추가

moz-firefox_javascript 기술에 IE 메소드 및 속성 추가

WBOY
WBOY원래의
2016-05-16 19:15:071049검색

IECN에서 선택 항목을 교환할 때 Xinyun이 작성한 JS를 봤습니다. RemoveNode 및 swapNode와 같은 메소드를 사용하기 때문에 Firefox에서는 유효하지 않습니다. 방금 Google에서 검색한 결과 사용자 정의 프로토타입을 통해 IE에서만 유효한 속성과 메서드를 수정할 수 있다는 것을 알았습니다.

원본 참조 : http://www.phpx.com/happy/top97619.html

수정 계획은 다음과 같습니다.


<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>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.