Js event listener usage example _ javascript skills

Source: Internet
Author: User
This article mainly introduces the js event listener usage, and analyzes in detail the precautions and related skills for using javascript event listeners in the form of instances, for more information about js event listeners, see the examples in this article. Share it with you for your reference. The specific analysis is as follows:

1. when the same object is used. when the onclick method triggers multiple methods, the latter method overwrites the previous method. that is to say, when the onclick event of an object occurs, only the last bound method is executed. However, event listening does not overwrite events, and each bound event is executed. As follows:

Window. onload = function () {var btn = document. getElementById ("yuanEvent"); btn. onclick = function () {alert ("First Event");} btn. onclick = function () {alert ("Second Event");} btn. onclick = function () {alert ("Third Event ");}}

Last output only: the third event, because the last method overwrites the previous method.

The original event binding function addEventListener:

Var eventOne = function () {alert ("first listener event");} function eventTwo () {alert ("second listener event");} window. onload = function () {var btn = document. getElementById ("yuanEvent"); // addEventListener: bind function btn. addEventListener ("click", eventOne); btn. addEventListener ("click", eventTwo );}

Output: the first listener event and the second listener event

2. after using event listening to bind an object, you can unbind it as follows:

Var eventOne = function () {alert ("first listener event");} function eventTwo () {alert ("second listener event");} window. onload = function () {var btn = document. getElementById ("yuanEvent"); btn. addEventListener ("click", eventOne); btn. addEventListener ("click", eventTwo); btn. removeEventListener ("click", eventOne );}

Output: second listener event

3. when unbinding an event, you must use the function handle to unbind the entire function.

Incorrect syntax:

btn.addEventListener("click",function(){   alert(11); }); btn.removeEventListener("click",function(){   alert(11); });

Correct syntax:

btn.addEventListener("click",eventTwo); btn.removeEventListener("click",eventOne); 

Summary: The listening events encapsulated by functions are as follows, which are compatible with major browsers.

/** AddEventListener: listens to Dom element events ** target: Listener object * type: Listener function type, such as click, mouseover * func: Listener function */function addEventHandler (target, type, func) {if (target. addEventListener) {// listen to IE9, Google and Firefox target. addEventListener (type, func, false);} else if (target. attachEvent) {target. attachEvent ("on" + type, func);} else {target ["on" + type] = func ;}/ ** removeEventHandler: event ** target: Listener object * type: Listener function type, such as click, mouseover * func: Listener function */function removeEventHandler (target, type, func) {if (target. removeEventListener) {// listen to IE9, Google, and Firefox target. removeEventListener (type, func, false);} else if (target. detachEvent) {target. detachEvent ("on" + type, func);} else {delete target ["on" + type] ;}} var eventOne = function () {alert ("first listener event");} function eventTwo () {alert ("second listener event");} window. onload = function () {var bindEventBtn = document. getElementById ("bindEvent"); // listen to eventOne event addEventHandler (bindEventBtn, "click", eventOne); // listen to eventTwo event addEventHandler (bindEventBtn, "click", eventTwo ); // listen to the event addEventHandler (bindEventBtn, "click", function () {alert ("third listener event ");}); // cancel the first listening event removeEventHandler (bindEventBtn, "click", eventOne); // cancel the second listening event removeEventHandler (bindEventBtn, "click", eventTwo );}

Instance:

         
      Event     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.