Detailed analysis of compatibility performance in JavaScript
1. document.form.item problem
Problem:
There are statements like
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">document.formName.item("itemName")<br/></span>
in the code , cannot run under FF
Solution:
Use instead
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">document.formName.elements["elementName"]<br/></span>
2. Collection class object problem
Problem:
Many collection objects in the code use () when accessing them. IE can accept it, but FF cannot.
Solution:
Use [] instead Subscript operation, for example:
document.getElementsByName("inputName")(1) 改为 document.getElementsByName("inputName")[1]
3. window.event
Problem:
Using window.event cannot run on FF
Solution:
FF's event can only be used at the scene where the event occurs. This problem cannot be solved yet. You can pass the event to the function to work around the solution:
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">onMouseMove = "functionName(event)"<br/>function functionName (e) {<br/> e = e || window.event;<br/> ......<br/>}<br/></span>
4. The problem of using the id of the HTML object as the object name
Problem:
InIn IE, the ID of the HTML object can be used directly as the subordinate object variable name of the document, but cannot be used in FF.
Solution:
When using object variables, all use the standard getElementById(" idName")
5. The problem of using the idName string to obtain the object
Problem:
In IE, you can use eval("idName") to obtain the object whose id is idName HTML objects cannot be used in FF
Solution:
Use getElementById("idName") instead of eval("idName")
6. Variable name and an HTML object The same problem with id
Question:
In FF, because the object id is not used as the name of the HTML object, you can use the same variable name as the HTML object id, but not in IE
Solution:
When declaring variables, always add var to avoid ambiguity, so that it can also run normally in IE
It is best not to get the HTML object id The same variable name to reduce errors
7. Event.x and event.y Problem
Problem:
InIE, the event object has x, y attributes , there is no
solution in FF:
In FF, the equivalent of event.x is event.pageX, but event.pageX does not have
in IE, so use event.clientX replaces event.x. There is also this variable in IE.
Event.clientX has a subtle difference with event.pageX, that is, the scroll bar
must be exactly the same. It can be like this:
mX = event.x ? event.x : event.pageX;
Then use mX instead of event.x
8. Regarding the problem of frame
:
在You can use window.testFrame to get the frame in IE, but not in FF
Solution:
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">window.top.document.getElementById("testFrame").src = 'xx.htm'<br/>window.top.frameName.location = 'xx.htm'<br/></span>
9. Get the attributes of the element
In FF, the attributes you define must be obtained by getAttribute()
10. In FF, there is no parentElement, parement.children and parentNode, parentNode.childNodes
Problem:
childNodes The meaning of the subscript is different in IE and FF. Blank text nodes will be inserted into the childNodes of FF
Solution:
You can avoid this problem by node.getElementsByTagName()
Problem:
When the node in html is missing, IE and FF interpret parentNode differently, for example:
<span style="font-family:'微软雅黑', 'Microsoft YaHei';"><form><br/><table><br/><input/><br/></table><br/></form><br/></span>
FF中 input.parentNode 的值为form,而IE中 input.parentNode 的值为空节点
问题:
FF中节点自己没有 removeNode 方法
解决方法:
必须使用如下方法 node.parentNode.removeChild(node)
11. const 问题
问题:
在IE中不能使用 const 关键字
解决方法:
以 var 代替
12. body 对象
FF的 body 在 body 标签没有被浏览器完全读入之前就存在,而IE则必须在 body 完全被读入之后才存在
这会产生在IE下,文档没有载入完时,在body上appendChild会出现空白页面的问题
解决方法:
一切在body上插入节点的动作,全部在onload后进行
13. url encoding
问题:
一般FF无法识别js中的&
解决方法:
在js中如果书写url就直接写&不要写&
14. nodeName 和 tagName 问题
问题:
在FF中,所有节点均有 nodeName 值,但 textNode 没有 tagName 值,在IE中,nodeName 的使用有问题
解决方法:
使用 tagName,但应检测其是否为空
15. 元素属性
IE下 input.type 属性为只读,但是FF下可以修改
16. document.getElementsByName() 和 document.all[name] 的问题
问题:
在IE中,getElementsByName()、document.all[name] 均不能用来取得 p 元素
是否还有其它不能取的元素还不知道(这个问题还有争议,还在研究中)
17. 调用子框架或者其它框架中的元素的问题
在IE中,可以用如下方法来取得子元素中的值
document.getElementById("frameName").(document.)elementName window.frames["frameName"].elementName
在FF中则需要改成如下形式来执行,与IE兼容:
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">window.frames["frameName"].contentWindow.document.elementName<br/>window.frames["frameName"].document.elementName<br/></span>
18. 对象宽高赋值问题
问题:
FireFox中类似 obj.style.height = imgObj.height 的语句无效
解决方法:
统一使用
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">obj.style.height = imgObj.height + "px";<br/></span>
19. innerText的问题
问题:
innerText 在IE中能正常工作,但是 innerText 在FireFox中却不行
解决方法:
在非IE浏览器中使用textContent代替innerText
20. event.srcElement和event.toElement问题
问题:
IE下,even对象有srcElement属性,但是没有target属性;Firefox下,even对象有target属性,但是没有srcElement属性
解决方法:
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">var source = e.target || e.srcElement;<br/>var target = e.relatedTarget || e.toElement;<br/></span>
21. 禁止选取网页内容
问题:
FF需要用CSS禁止,IE用JS禁止
解决方法:
IE:
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">obj.onselectstart = function() {return false;}<br/></span>
FF: -moz-user-select:none;
22. 捕获事件
问题:
FF没有setCapture()、releaseCapture()方法
解决方法:
IE:
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">obj.setCapture(); <br/>obj.releaseCapture();</span>
FF:
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
<span style="font-family:'微软雅黑', 'Microsoft YaHei';">window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);<br/>if (!window.captureEvents) {<br/> o.setCapture();<br/>}else {<br/> window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);<br/>}<br/>if (!window.captureEvents) {<br/> o.releaseCapture();<br/>}else {<br/> window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);<br/>}</span>
The above is the detailed content of Detailed analysis of compatibility performance in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
