adding event as parameter within function, using addEventListener (Doesn't work in FF/IE). Javascript

后端 未结 2 2127
滥情空心
滥情空心 2021-02-08 21:56


FML



        
2条回答
  •  情话喂你
    2021-02-08 22:35

    In some browsers like Firefox the event has to be passed as a parameter, so you have to replace this code

    div.addEventListener("mousemove", function(){test(event, this)}, true);
    

    with this

    div.addEventListener("mousemove", function(event){test(event, this)}, true);
    

    Compare carefully the two lines of code and notice the event property passed as parameter of the function added into the second row.

    for old IE ver attachEvent() has to be used;

    div.attachEvent("onclick", function(event){test(event, this)});
    

    Notice that attachEvent has been replaced in modern standard js with addEventListener and now almost all modern browser support that.

    Here http://caniuse.com/#feat=addeventlistener is possible to see compatibility table.

提交回复
热议问题