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

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. . | Handling Link Events 481 Figure Spring image top summer image middle and fall image bottom are all part of the slideshow created in Example . 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 . Table 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 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 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 . EXAMPLE html head title Deactivate the hotlink title head body center a href onClick alert This hotlink is out

TỪ KHÓA LIÊN QUAN