Emulate W3C event capturing model in IE

爱⌒轻易说出口 提交于 2019-12-03 06:12:45

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

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.

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!

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()

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

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;});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!