tailieunhanh - Học JavaScript qua ví dụ part 77

Sự kiện người nghe với Microsoft Internet Explorer Microsoft cũng đã phát triển một mô hình đăng ký sự kiện. Nó trông giống như W3C, nhưng có một số khác biệt quan trọng. Để viết mã nền tảng tương thích, bạn có thể thử nghiệm cho cả hai. | 676 Chapter 15 The W3C DOM and JavaScript Figure The event listener was removed. The mouse event doesn t occur. Event Listeners with Microsoft Internet Explorer Microsoft too has developed an event registration model. It looks similar to W3C s but has some important differences. To write cross-platform compatible code you can test for both. See Example . The attachEvent Method. The attachEvent method is an IE5 proprietary equivalent of addEventListenerQ. The method is attached to the object for which the event is intended. The parameters include the event type and a function. Unlike the W3C model the event type parameter must include the on prefix . onload onclick etc. . If you forget this little but important point the method will not work FORMAT eventType function EXAMPLE if myDiv div1 onclick changecolor From the Library of Event Listeners with the W3C Model 677 The detachEvent Method. The detachEvent method removes an event handler and its function and is the IE5 proprietary equivalent of the DOM s removeEventListener . FORMAT eventType function EXAMPLE if onload init There are differences you should be aware of when comparing the W3C model and the Internet Explorer model 1. Events always bubble. There is no capturing feature. 2. The this keyword cannot be used to represent the event handling function. The this keyword refers to the global window object. See Example for examples of Internet Explorer handling. EXAMPLE html head title Internet Explorer Event Handling title script type text javascript 1 function greetings alert Hello World Add an event handler 2 onload greetings Add another event handler 3 onclick greetings Add another event handler onmouseover greetings 4 Remove an event handler just added 5 .

TỪ KHÓA LIÊN QUAN