Emulate W3C event capturing model in IE

前端 未结 6 1355
野趣味
野趣味 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:17

    IE has a Element.setCapture() method that you may find useful http://msdn.microsoft.com/en-us/library/ms536742(v=vs.85).aspx It allows you to route all mouse events to the element that called setCapture()

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-07 12:19

    Generally you can't because of the event order. In IE the events will start bubbling from the target element without the capturing phase so you can't catch them beforehand.

    There's only one thing you can do, and it's only possible if you manage all the event handlers.

    1. Write a wrapper for addEvent with capture parameter
    2. If capturing is required do the following

      1. Register a simple bubbling event, with a function that
      2. Goes all the way up the parent chain, saving all the elements to an Array
      3. Travels backward this Array invoking the original event handler on each of the elements
    0 讨论(0)
  • 2021-02-07 12:24

    The Uniform Event Model project from JavaScript Lab appears to emulate the capture phase. Go to the download page for the JSLab DOM Correction library and select everything and select the commented format. Then download the code and search it for the word 'capture'. I have not tested the library or read much of its code.

    0 讨论(0)
  • 2021-02-07 12:34

    The best way if you use only bubbling for clicks:

    if (document.addEventListener) document.addEventListener("click", function(e){e.preventDefault();},false);
    else if (document.attachEvent) document.attachEvent("onclick", function(){window.event.returnValue = false;});
    
    0 讨论(0)
  • 2021-02-07 12:39

    setCapture is used to retain some mouse-related action outside the browser window

    and it is used to implement some kind of drag&drop

    if you mousedown an element and you will go with your pointer outside the browser window the mousemove event stops to work

    if you setCapture() the mousemove event will continue to work outside the browser window

    https://developer.mozilla.org/en/DOM/element.setCapture

    and the related method to release capture

    https://developer.mozilla.org/en/DOM/document.releaseCapture

    so, it has nothing in common with the capturing event model and, there's no known way to emulate it in internet explorer in a standard way!

    hope this helps!

    0 讨论(0)
提交回复
热议问题