Home Web Front-end JS Tutorial javascript 浏览器兼容性代码一览表

javascript 浏览器兼容性代码一览表

Jun 01, 2016 am 09:54 AM
javascript compatibility

序号

操作

分类

IE
(6.0)

FireFox
(2.0)

Mozilla
(1.5)

当前
浏览器

备注

1

"."

访问tag的固有属性

OK

OK

OK

OK

 

2

"."

访问tag的用户定义属性
eg: myattr="test">

OK

NO

NO

OK

可以用getAttribute函数 替代

3

obj.getAttribute

访问tag的固有属性

OK

OK

OK

OK

 

4

obj.getAttribute

访问tag的用户定义属性
eg: myattr="test">

OK

OK

OK

OK

 

5

document.all

访问document的所有子元素
eg:document.all

OK

OK

NO

OK

建议用childNodes对象或者getElementById函数实现对应操作。
有人说它不能取Div元素,测试结果是可以,不知道是不是还有其它因素会影响测试结果。

6

obj.all

访问非document元素的所有子元素
eg: document.getElementById("mydiv").all

OK

NO

NO

OK

同上

7

getElementById()

根据元素的id/name来取得元素。
如果元素只设置name属性,没有设置id属性。

OK

NO

NO

OK

注意:很多元素是没有name属性的,eg: td, div,span...

8

变量名 = ""

隐式定义变量-通过向变量名附值方式定义一个新的变量。

OK

OK

OK

OK

建议:为避免必要的麻烦,显示定义变量。
eg:var tmp;

9

id

通过id直接调用对象
eg: test_result_1.innerHTML = ""

OK

OK

NO

OK

eval()函数用来执行脚本,所以向eval函数里面传入对象id/name的话,IE同样会返回对象的引用。
建议用document.getElementById(id)方式调用
注意:因为这个原因,IE中隐式定义的变量不能和HTML中元素的id/name相同。

10

name

通过name直接调用对象
eg: test_for_this_name.innerHTML = ""

OK

NO

NO

OK

同上/ 原因同7

11

name

支持的特殊字符("!",".","@","#","$"," eg: document.getElementsByName("aaaa!page");

NO

OK

OK

NO

其它的字符没有测试

12

tr.innerHTML = ""

设置TR元素的内部HTML脚本

NO

OK

OK

NO

在IE中,table、tr的innerHTML是只读属性,不能够对其直接设置。可以通过insertRow/insertCell函数进行设置。

13

cells对象访问

访问tr的cells对象
前提:tr元素是被删除后的tr对象,或者是用cloneNode(true)复制的删除前的tr对象

NO

OK

OK

undefined

可以将其插入Table再访问,或者可以用getElementsByTagName函数 对td/th访问。
删除后的rows对象不存在这个问题。其它元素?

14

(index)

访问集合类对象
eg: document.
getElementsByTagName("TD")(0)

OK

NO

NO

OK

建议用正式的操作符"[]".

15

obj.toString()

取得对象的字符串"[object 对象类型的名称]".
eg: td == "[object HTMLTableCellElement]"

NO

OK

OK

NO

可以省略toString()函数,直接用对象来操作。
注:在IE中返回"[object]"。

16

obj.class

定义对象的css式样/风格。
eg: td.class="XPstyle";

NO

OK

NO

-

无法写测试代码,会有编译错误!可以自己用typeof(class) == null来测试一下,没有异常就在代码中可以使用。
在HTML脚本中用class,但是在Javascript中应该用classname(class是JS的保留关键字).
注意:用 setAttribute可以把class值设置到对象中去,但是不会被当作css式样来解析。

17

const

保留关键字,用于定义常量。
eg:const HOURS = 24;

NO

OK

OK

-

暂时只能不使用const。

18

input.type

变更input元素的类型
eg: input.type="button";

NO

OK

OK

NO

IE可以初始input元素类型,但是不能变更类型。
如果必须变更,可以用更换input元素的方式。

19

obj.children

访问对象的子元素集合
eg: document.body.children.length;

OK

NO

NO

OK

可以用childNodes对象替代。

20

node.replaceNode

替换新的节点对象
eg: oldNode.replaceNode(newNode);

OK

NO

NO

OK

可以用replaceChild函数替换。

21

node.removeNode

删除已有节点对象
eg.oldNode.removeNode(true);

OK

NO

NO

OK

可以用oldNode.parentNode.removeChild(oldNode)方式实现。

22

node.insertBefore

在指定节点对象前面插入一个节点对象
document.body.insertBefore(newN, oldN);

OK

OK

OK

OK

 

23

obj.parentElement

访问对象的父元素
eg: document.body.parentElement.id;

OK

NO

NO

OK

可以用parentNode对象替代。

24

obj.childNodes.length

返回子节点的数量,和tag的数量相同。
eg:document.body.childNodes.length;

OK

NO

NO

OK

FF/Mozilla中,空白或者换行是文本节点,是childNodes的成员。
可以用node.getElementsByTagName()回避。

25

obj.
insertAdjacentElement

向指定的位置插入元素
eg: obj.insertAdjacentElement("beforeBegin",button);

OK

NO

NO

OK

insertAdjacentElement函数和insertAdjacentText函数也类似。
可以用insertBefore函数实现类似功能。

26

createElement()

创建指定类型元素。
前提:元素为HTML脚本
eg:document.createElment("");

OK

NO

NO

OK

可以先创建出对象元素,再进行属性设置;或者直接以InnerHTML的形式加到对应位置。
 

27

nodeName

取对象(tag,attribute,textnode)节点名称
eg: name = obj.nodeName;

OK

OK

OK

OK

有人说存在差异,不知道是具体的前提条件,先记录备考。
替代方案:
如果节点是tag元素可以用"tagName"取值;如果节点是attribut对象可以用"name"取值;如果节点是textnode元素可以用nodeType==3判断。

28

window.event

取得当前的事件对象
eg: window.event;

OK

NO

NO

?

可以主动向事件的响应函数传入event参数。
eg:help

29

event.target

取得事件的触发对象
eg: e.target;

NO

OK

OK

可以和srcElement共同使用;可以主动向事件的响应函数传入触发对象元素。
eg:help

30

event.srcElement

取得事件的触发对象
eg: e.srcElement;

OK

NO

NO

可以和target共同使用;可以主动向事件的响应函数传入触发对象。
eg: var obj = (e.target) ? e.target : e.srcElemtn;

31

event对象属性

当前三个浏览器的共同拥有的属性:
altKey
button
cancelBubble
clientX
clientY
ctrlKey
screenX
screenY
shiftKey
type

 

altLeft
behaviorCookie
behaviorPart
bookmarks
boundElements
contentOverflow
ctrlLeft
dataFld
dataTransfer
fromElement
keyCode
nextPage
offsetX
offsetY
propertyName
qualifier
reason
recordset
repeat
returnValue
srcElement
shiftLeft
srcFilter
srcUrn
toElement
wheelDelta
x
y
 

bubbles
cancelable
currentTarget
detail
eventPhase
explicitOriginalTarget
isChar
isTrusted
layerX
layerY
metaKey
originalTarget
pageX
pageY
rangeOffset
rangeParent
relatedTarget
target
timeStamp
view
which

bubbles
cancelable
charCode
currentTarget
detail
eventPhase
explicitOriginalTarget
isChar
keyCode
layerX
layerY
metaKey
originalTarget
pageX
pageY
popupWindowURI
rangeOffset
rangeParent
relatedTarget
requestingWindowURI
target
timeStamp
view
which

?

event对象的不同点太多,在使用的时候需要一一检查才行。具体可以用页面下方的测试区域试验。

32

注册event

用attachEvent函数注册

OK

NO

NO

-

小心内存泄漏!!!
事件处理完后一定要调用detachEvent函数销毁事件,而且要注意避免重复注册。

33

注册event

addEventListener函数注册

NO

OK

OK

-

 

34

注册event

obj.onxxx = Function("响应函数名称或代码");方式注册
eg: btn.onclick = Function(doclick);
btn.onclick = Function("return 1+1;");

OK

OK

OK

-

 

35

销毁event

detachEvent函数销毁

OK

NO

NO

-

 

36

销毁event

removeEventListener函数销毁

NO

OK

OK

-

 

37

销毁event

obj.onxxx = null;方式注册
eg: btn.onclick = null;

OK

OK

OK

-

 

38

触发event

fireEvent函数
eg:btn.fireEvent("onclick");
FF:
var e = document.createEvent("Events"); 
e.initEvent("click", true, false); 
element.dispatchEvent(event)

OK

NO

NO

-

 

39

触发event

dispatchEvent函数
eg: 
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true);
btn.dispatchEvent(evt);

NO

OK

OK

-

 

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Comparison and difference analysis of Bluetooth 5.3 and 5.2 versions Comparison and difference analysis of Bluetooth 5.3 and 5.2 versions Dec 28, 2023 pm 06:08 PM

Nowadays, many mobile phones claim to support Bluetooth 5.3 version, so what is the difference between Bluetooth 5.3 and 5.2? In fact, they are essentially subsequent updated versions of Bluetooth 5, and there is not much difference in most performance and functions. The difference between Bluetooth 5.3 and 5.2: 1. Data rate 1 and 5.3 can support higher data rates up to 2Mbps. 2. While 5.2 can only reach a maximum of 1Mbps, it means that 5.3 can transmit data faster and more stably. 2. Encryption control enhancement 2. Bluetooth 5.3 improves encryption key length control options, improves security, and can better connect to access control and other devices. 3. At the same time, because the administrator control is simpler, the connection can be more convenient and faster, which is not possible in 5.2.

Solution to i7-7700 unable to upgrade to Windows 11 Solution to i7-7700 unable to upgrade to Windows 11 Dec 26, 2023 pm 06:52 PM

The performance of i77700 is completely sufficient to run win11, but users find that their i77700 cannot be upgraded to win11. This is mainly due to restrictions imposed by Microsoft, so they can install it as long as they skip this restriction. i77700 cannot be upgraded to win11: 1. Because Microsoft limits the CPU version. 2. Only the eighth generation and above versions of Intel can directly upgrade to win11. 3. As the 7th generation, i77700 cannot meet the upgrade needs of win11. 4. However, i77700 is completely capable of using win11 smoothly in terms of performance. 5. So you can use the win11 direct installation system of this site. 6. After the download is complete, right-click the file and "load" it. 7. Double-click to run the "One-click

How compatible is the Go language on Linux systems? How compatible is the Go language on Linux systems? Mar 22, 2024 am 10:36 AM

The Go language has very good compatibility on Linux systems. It can run seamlessly on various Linux distributions and supports processors of different architectures. This article will introduce the compatibility of Go language on Linux systems and demonstrate its powerful applicability through specific code examples. 1. Install the Go language environment. Installing the Go language environment on a Linux system is very simple. You only need to download the corresponding Go binary package and set the relevant environment variables. Following are the steps to install Go language on Ubuntu system:

Detailed explanation of win11 compatibility issues with win10 software Detailed explanation of win11 compatibility issues with win10 software Jan 05, 2024 am 11:18 AM

The software in the win10 system has been perfectly optimized, but for the latest win11 users, everyone must be curious about whether this system can be supported, so the following is a detailed introduction to the win11 software that does not support win10. Come and find out together. Does win11 support win10 software: 1. Win10 system software and even Win7 system applications are well compatible. 2. According to feedback from experts who use the Win11 system, there are currently no application incompatibility issues. 3. So you can upgrade boldly with confidence, but ordinary users are advised to wait until the official version of Win11 is released before upgrading. 4. Win11 not only has good compatibility, but also has Windo

Can I use Bluetooth headphones in airplane mode? Can I use Bluetooth headphones in airplane mode? Feb 19, 2024 pm 10:56 PM

With the continuous development of modern technology, wireless Bluetooth headsets have become an indispensable part of people's daily lives. The emergence of wireless headphones frees our hands, allowing us to enjoy music, calls and other entertainment activities more freely. However, when we fly, we are often asked to put our phones in airplane mode. So the question is, can I use Bluetooth headphones in airplane mode? In this article, we will explore this question. First, let’s understand what airplane mode does and means. Airplane mode is a special mode for mobile phones

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

WIN10 compatibility lost, steps to recover it WIN10 compatibility lost, steps to recover it Mar 27, 2024 am 11:36 AM

1. Right-click the program and find that the [Compatibility] tab is not found in the properties window that opens. 2. On the Win10 desktop, right-click the Start button in the lower left corner of the desktop and select the [Run] menu item in the pop-up menu. 3. The Win10 run window will open, enter gpedit.msc in the window, and then click the OK button. 4. The Local Group Policy Editor window will open. In the window, click the [Computer Configuration/Administrative Templates/Windows Components] menu item. 5. In the opened Windows component menu, find the [Application Compatibility] menu item, and then find the [Remove Program Compatibility Property Page] setting item in the right window. 6. Right-click the setting item, and in the pop-up menu

Best practices for resolving PHP function compatibility issues Best practices for resolving PHP function compatibility issues May 01, 2024 pm 02:42 PM

Best practices to solve PHP function compatibility issues: Use versioned function names (for example: array_map_recursive()) Leverage function aliases (for example: functionarray_map($callback,$array){...}) to check function availability (for example: if (function_exists('array_map_recursive')){...}) use namespace (for example: namespaceMyNamespace{...})

See all articles