JS AddEventListener RemoveEventListener

Source: Internet
Author: User

AddEventListener ,RemoveEventListener

    • AddEventListener () accepts three parameters.
      The first is the event type, such as click,keypress, the idea is not on , and it is all lowercase.
      The second is the handler function, which has an event parameter, and theevent includes the type, target, andso on.
      The third is the bool value, not the write is false, False is thebubbling event handler, andTrue is the capture event handler function.
    • RemoveEventListener() accepts three parameters.
      The first is the event type, ibid.
      The second is the processing function, must be registered with the same function, the anonymous function is useless. such as AddEventListener is fun1, RemoveEventListener also if fun1
      The third one ibid.
        var  father = document.getElementById ("Father"     var  fatherhandle = function   '---------Father-----------'  "Click", Fatherhandle, false  ); Father.removeeventlistener ( "Click", Fatherhandle, false ); //  valid  Father.removeeventlistener ("click", function  () {Console.log ('--no--')}, false ); //  invalid  
    • multiple handlers for the same event type can be registered for the same object by calling AddEventListener multiple times, and called in the order in which they are registered.
      However, multiple registrations with the same parameters are not valid and will only be registered once.
    • Within the event handler, this is thenode object that points to the event target
    • The processing function return False is to block the default action,
      Preventdefault () of the event object in AddEventListenercancels the default action
    • Three stages of event propagation:
      Capture of events: internal objects that are triggered by the Window object
      Event handler: The third parameter is true for the capture function, andFalse for the bubbling handler function
      Event bubbling: Spreads from the innermost object to the root node outwards.
      The Stoppropagation () method that invokes the event object prevents the event from bubbling. If other functions are registered on the same object, they will continue to be called, but no more will be performed on the other objects.
      Stopimmediatepropagation () prevents other objects from propagating and also prevents other functions of the same object from executing.

varFather = document.getElementById ("Father"); varChild = document.getElementById ("Child"); varFatherhandle =function(event) {Console.log ('---------Father-----------')    }    varChildhandle =function(event) {Console.log ('---------child-----------')} father.addeventlistener ("Click", Fatherhandle,true); Child.addeventlistener ("Click", Childhandle,true); //Father-child
    false );    Child.addeventlistener (false);     // Child-father
var fatherhandle = function (event) {Console.log ('---------Father-----------')} var childhandle = function (event) {Console.log ('---------child-----------') Event.stoppropagation ()}
Father.addeventlistener ("Click", Fatherhandle, false);    Child.addeventlistener ("Click", Childhandle, false);    Child.addeventlistener ("click", Function (e) {console.log (' haha ')}, False); Child haha

Call Order

      • To set an object property or HTML property to register a function first call
      • AddEventListener called in registered order
      • IE's attachevent () is not called sequentially, so the code should not be called since

<Body>    <H1ID= "Father">Father<spanID= "Child"onclick= "Fun ()">Child</span>    </H1><Script>    var Child=document.getElementById (" Child"); varChildhandle= function(event) {Console.log ('---------Child-----------')} child.addeventlistener ("Click", Childhandle,false); var Fun= function(e) {Console.log ('---------Property-----------')    }    //Property-child</Script></Body>

JS AddEventListener RemoveEventListener

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.