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

Mô hình Scripting để xử lý sự kiện Chúng tôi đã được sử dụng như xử lý sự kiện onClick, onSubmit, và onmouseover, trong văn bản này. Cho đến nay, chương này đã mô tả chi tiết tất cả các xử lý sự kiện khác nhau và làm thế nào để sử dụng chúng như nội tuyến thuộc tính HTML. Inline xử lý sự kiện là cách lâu đời nhất và đơn giản nhất để xử lý sự kiện và là trình duyệt tương thích | The Scripting Model for Handling Events 517 The Scripting Model for Handling Events We have been using event handlers like onClick onSubmit and onMouseOver throughout this text. So far this chapter has described in detail all of the different event handlers and how to use them as inline HTML attributes. Inline event handling is the oldest and simplest way to handle events and is browser compatible. The following example uses the onClick handler as an attribute of the button element. When the user clicks the button the function movePosition will be called. input type button value move text onClick movePosition But using this type of handler violates the principle of separation of the layers that is the separation of markup presentation from behavior JavaScript. To solve this problem we can handle events within the JavaScript code itself. All of the HTML attributes used as event handlers can also be used as DOM properties. These properties can be used in JavaScript to simulate the event that they are named for. If for example you want to trigger a window event JavaScript views the window as an object and any event associated with it as a property. If you want to use the onload event with the window object you would say . The main difference is that unlike using HTML attributes which take a string value a function reference is assigned to an event handler. All JavaScript event properties must be in lowercase such as or . The event handlers used as HTML attributes are not case sensitive so ONUNLOAD onUnLoad and onunload are all acceptable. Here s an example some_function Getting a Reference to the Object To assign an event property to an object JavaScript will need a reference to the object. For example if the click event is to be triggered when the user clicks a button then JavaScript will need to use a reference to the button. By assigning an id to the HTML button JavaScript can use the DOM s .

TỪ KHÓA LIÊN QUAN