javascript键盘事件兼容
2015-10-31 23:41
633 查看
JavaScript Code
window.event;是ie的 传参数e是w3c的
keypress + charCode 是火狐、谷歌(可以输入大小写)
keypress +keyCode 是ie(可以输入大小写)
JavaScript Code
srcElement是ie
target是火狐
阻断冒泡
JavaScript Code
cancelBubble ie浏览器 stopPropagation 火狐 谷歌
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); } } |
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); } } |
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(); }; } |
相关文章推荐
- JS学习笔记(01)—— 基础
- JS正则表达式验证账号、手机号、电话和邮箱
- javascript中讨论关联数组和对象的区别
- js刷新当前页的方法
- JS之简易计算器
- iScroll.js 向上滑动异步加载数据回弹问题
- JS原生Date类型方法
- JavaScript 阶段总结
- Echarts心得
- JSON 和 List、Map集合结合使用
- c# json操作
- jsp获取contextPath的方法
- 掌握XML、JSON及其解析教程
- javascript 实现文本框明文转暗码
- Windows 10开发基础——XML和JSON (一)
- angular-file-upload 中文API
- 超轻量级异步JS框架
- 10个强大的JavaScript库
- JSON
- JavaScript类继承详解