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

May 16, 2016 pm 07:15 PM

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>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles