Emulate W3C event capturing model in IE

前端 未结 6 1353
野趣味
野趣味 2021-02-07 11:54

Is it possible to emulate event capturing in Internet Explorer?

An example:

one
two
three3



        
6条回答
  •  暖寄归人
    2021-02-07 12:18

    function myFunction(e) {
       if (!e) var e = window.object;//legacy event object
       if (e.preventDefault) e.preventDefault();//prevent firing in W3C model
       return false; //exit event, no firing, listener must registered to anchor tag
    }    
    var x = document.getElementsByTagName("A");
    
    if (x.item(0).addEventListener) { 
        for (var i = 0, l = x.length; i < l; i++) {
            x.item(i).addEventListener("click",myFunction,false);
        }
    }//W3C model
    else if (x.item(0).attachEvent) {
        for (var i = 0, l = x.length; i < l; i++) {
            x.item(i).attachEvent("onclick",myFunction);
            }
        }// legacy browsers
    

提交回复
热议问题