jsカスタムイベント

925 ワード

三つのカスタムイベント方式
  
        var event = new Event('myEvent');
        document.addEventListener("myEvent", function (e) {
            console.log(e);
        })
        document.dispatchEvent(event);

           var event = new CustomEvent('myEvent', { 'dataName': "ss" });
        document.addEventListener("myEvent", function (e) {
            console.log(e);
        })
        document.dispatchEvent(event);


        document.body.addEventListener("veb", function (e) {
            alert(e.eventType)
        })
        var event = document.createEvent('HTMLEvents');
        // initEvent  3   :
        //     ,    ,            
        event.initEvent("veb", false, true);
        //  eventType      
        event.eventType = "I love Veblen"
        //  document    click  
        document.body.dispatchEvent(event);