Đang chuẩn bị liên kết để tải về tài liệu:
Học JavaScript qua ví dụ part 53
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Trong rất nhiều các ví dụ trước, các liên kết đã được sử dụng để kích hoạt sự kiện. Khi người dùng nhấp chuột hay di chuyển chuột trên một liên kết, một sự kiện liên kết được kích hoạt. Một liên kết sự kiện, onClick, được gửi bất cứ khi nào ai đó nhấp chuột vào một liên kết. Như chúng ta đã thấy với onmouseover chuột, các sự kiện và onMouseOut cũng gây ra một sự kiện liên kết để xảy ra. . | 13.5 Handling Link Events 481 Figure 13.13 Spring image top summer image middle and fall image bottom are all part of the slideshow created in Example 13.11. 13.5 Handling Link Events In many of the previous examples links have been used to trigger events. When the user clicked or moved the mouse over a link a link event was triggered. One link event onClick gets sent whenever someone clicks on a link. As we saw with mouse events onMouseOver and onMouseOut also cause a link event to occur. The link events are listed in Table 13.5. Table 13.5 Link Events Event Handler When It Is Triggered onClick When the mouse is clicked on a link onMouseOut When a mouse is moved out of a link onMouseOver When a mouse is moved over a link 13.5.1 JavaScript URLs We have seen JavaScript code in a JavaScript URL throughout this text. In the example using mouse events the event handler was assigned to a link and the link was deactivated by assigning a quoted hash mark to the link href attribute From the Library of WoweBook.Com 482 Chapter 13 Handling Events a href onClick alert This hotlink is out of service return false Click here a or by using the JavaScript protocol followed by the void operator to guarantee that any return value from the function will be discarded a href JavaScript void 0 onMouseOver return changeSeason In either case the link was not supposed to take the user to another location but instead to handle an event or call a function. Make sure that any function calls in the URL have been defined. Another note If the causes the browser to jump to the top of the page when the link is clicked you can add a return false statement inside the onClick handler to keep the browser from checking the content of the href. The following simple example uses the onClick event handler with a deactivated link and the return statement the display is shown in Figure 13.14. EXAMPLE 13.12 html head title Deactivate the hotlink title head body center a href onClick alert This hotlink is out