14

I have an interesting question about event capturing, bubbling and jQuery.on().

I have recently learned more about the difference between event capturing and event bubbling and how the two flow differently in and out of the child-parent elements within the DOM.

So to add an event listener with "event capture direction" i would use:

element.addEventListener("click", myFunction, true);

and to add an event listener with "event bubble direction" i would use:

element.addEventListener("click", myFunction, false);

This is all good and well, but what I want to know is when using jquery.on() to add event listeners, how does one specifiy the event direction in terms of capturing and bubbling?

currently I am using something like:

$('parent selector').on('click', 'child selector', function(){alert('just alert something already...');});

How do I tell jQuery that it needs to add these event listeners in the "event capture direction" or the "event bubble direction"?

4

2 回答 2

20

你不能。jQuery 事件适用于不支持捕获的事件冒泡。

另见

于 2014-02-10T15:17:05.507 回答
0

您可以使用 e.target 来获取被点击的确切元素

  $('.parent').on('click', function(e){
    if(e.target.classList.contains('child'))
      // do something
    }
  })
于 2015-08-30T14:10:13.447 回答