Home > Web Front-end > JS Tutorial > body text

Detailed analysis of compatibility performance in JavaScript

黄舟
Release: 2017-10-21 11:26:05
Original
1650 people have browsed it


1. document.form.item problem
Problem:
There are statements like

<span style="font-family:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">document.formName.item("itemName")<br/></span>
Copy after login

in the code , cannot run under FF
Solution:
Use instead

<span style="font-family:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">document.formName.elements["elementName"]<br/></span>
Copy after login


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]
Copy after login

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:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">onMouseMove = "functionName(event)"<br/>function functionName (e) {<br/>    e = e || window.event;<br/>    ......<br/>}<br/></span>
Copy after login

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:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">window.top.document.getElementById("testFrame").src = &#39;xx.htm&#39;<br/>window.top.frameName.location = &#39;xx.htm&#39;<br/></span>
Copy after login

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:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;"><form><br/><table><br/><input/><br/></table><br/></form><br/></span>
Copy after login

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
Copy after login

FF中则需要改成如下形式来执行,与IE兼容:

<span style="font-family:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">window.frames["frameName"].contentWindow.document.elementName<br/>window.frames["frameName"].document.elementName<br/></span>
Copy after login

18. 对象宽高赋值问题
问题:
FireFox中类似 obj.style.height = imgObj.height 的语句无效
解决方法:
统一使用

<span style="font-family:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">obj.style.height = imgObj.height + "px";<br/></span>
Copy after login

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:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">var source = e.target || e.srcElement;<br/>var target = e.relatedTarget || e.toElement;<br/></span>
Copy after login

21. 禁止选取网页内容
问题:
FF需要用CSS禁止,IE用JS禁止
解决方法:
IE:

<span style="font-family:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">obj.onselectstart = function() {return false;}<br/></span>
Copy after login


FF: -moz-user-select:none;
22. 捕获事件
问题:
FF没有setCapture()、releaseCapture()方法
解决方法:
IE:

<span style="font-family:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">obj.setCapture(); <br/>obj.releaseCapture();</span>
Copy after login

FF:
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

<span style="font-family:&#39;微软雅黑&#39;, &#39;Microsoft YaHei&#39;;">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>
Copy after login

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!