function document_onKeyDown(){
if (window.attachEvent) {
//IE and Opera, IE on keydown event need use document element
document.attachEvent("onkeydown", receivablePanel_onKeyDown);
return true;
} else if (window.addEventListener) {
window.addEventListener("keydown", receivablePanel_onKeyDown, true);
return true;
} else {
document.addEventListener("keydown", receivablePanel_onKeyDown, true);
return true;
}
}
首先是判断用的是什么浏览器.每个浏览器都有自己的注册事件的方法.一般标准的注册方法是addEventListener;而ie采用了不标准的方法attachEvent.所以通过判断有没有这2个方法可以简单的判断出用的是IE还是标准的(FF,chrome).
PS:IE9也可以采用addEventListener的方式来注册.这是新加的
http://msdn.microsoft.com/en-us/library/ff975245(v=vs.85).aspx
然后就是需要注意下用attachEvent注册的时候event name必须写全,也就是onxxx,这个是microsoft的标准.
最后需要注意的是addEventListener的第3个参数,useCapture.
官方给出的解释是:
As to the true or false that is the last argument of addEventListener, it is meant to state whether the event handler should be executed in the capturing or in the bubbling phase. If you’re not certain whether you want capturing or bubbling, use false (bubbling).
http://www.quirksmode.org/js/events_advanced.html
如果希望handler method在冒泡中执行就传false,希望被捕获执行就用true.冒泡的意思是按照event order依次执行.这会带来一定的困惑.有的时候不需要执行的事就会去执行,譬如form的entry submit.
分享到:
相关推荐
+ [enterprise] added property "BrowserPrint" in server configuration - allow printing by browser, added new template nav_print_browser.html + [enterprise] added dynamic file name generation of ...
Known for its simplicity and efficiency, JQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions. The library's core philosophy is to make complex tasks simple ...
Cross-Language Support 353 The CTS and the CLS 353 Language Independence in Action 354 CLS Requirements 364 xv Contents Global Assembly Cache 366 Native Image Generator 366 Global Assembly Cache ...
PEP 479: Change StopIteration handling inside generators PEP 485: A function for testing approximate equality PEP 486: Make the Python Launcher aware of virtual environments PEP 488: Elimination of...
Cross Platform ..........................................................................................................11 Broad User Base ...............................................................