您的位置:首页 > Web前端 > JavaScript

javascript键盘事件兼容

2015-10-31 23:41 633 查看
JavaScript Code

1
2

3

4

5

6

7

8

9

window.onload=function()

{

document.onkeypress=function(e)

{

var ent=e||window.event;

alert(ent.charCode?ent.charCode:ent.keyCode);

}



}
window.event;是ie的 传参数e是w3c的

keypress + charCode 是火狐、谷歌(可以输入大小写)
keypress +keyCode 是ie(可以输入大小写)

JavaScript Code
1

2

3

4

5

6

7

8

9

10

11

12

window.onload=function()

{

document.onclick=function(e)

{

var ent=e||window.event;

alert( ent.target||ent.srcElement);



}





}
srcElement是ie
target是火狐

阻断冒泡

JavaScript Code
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

// JavaScript Document

window.onload=function()

{

document.onclick=function()

{

alert('我是document');

};

document.documentElement.onclick=function()

{

alert('我是html');

};

document.body.onclick=function()

{

alert('我是body');

};

document.getElementById('box').onclick=function()

{

alert('我是div');

};

document.getElementsByTagName('input')[0].onclick=function(e)

{

alert('我是input');



ent=e||window.event;

window.event?ent.cancelBubble=true:ent.stopPropagation();

};





}
cancelBubble ie浏览器 stopPropagation 火狐 谷歌
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: