最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Bubbling and capturing with addEventListener - Stack Overflow

matteradmin3PV0评论

I recently discovered the difference between Bubbling and Capturing on DOM events, using javascript. Now I understand how it's supposed to work, but I've found a weird situation and I would like to know why is that happening.

According to Quirks mode, the event propagation starts with capturing on the outer div, reaches the bottom and then bubbles up to the top. The problem was when I started doing some tests.

On this first one, everything works as expected:

<div id="out">
    <div id="in">
        Click This!!
    </div>
</div>
<script type="text/javascript">
    document.getElementById('out').addEventListener('click', function(){
        alert('capture out');
    }, true);
    document.getElementById('in').addEventListener('click', function(){
        alert('capture in');
    }, true);
    document.getElementById('out').addEventListener('click', function(){
        alert('bubble out');
    }, false);
    document.getElementById('in').addEventListener('click', function(){
        alert('bubble in');
    }, false);
</script>

If you click the text, the alerts go 'capture out', 'capture in', 'bubble in' and 'bubble out'. The problem is using the following code:

<div id="out">
    <div id="in">
        Click This!!
    </div>
</div>
<script type="text/javascript">
    document.getElementById('out').addEventListener('click', function(){
        alert('bubble out');
    }, false);
    document.getElementById('in').addEventListener('click', function(){
        alert('bubble in');
    }, false);
    document.getElementById('out').addEventListener('click', function(){
        alert('capture out');
    }, true);
    document.getElementById('in').addEventListener('click', function(){
        alert('capture in');
    }, true);
</script>

In this case the alerts go 'capture out', 'bubble in', 'capture in' and 'bubble out'. If you notice, the only difference is that on the second one the bubbling is assigned first, but I don't think that should make any difference.

I've tried this with Firefox and Chrome, and the results are the same (I understand that internet explorer doesn't handle capturing).

I recently discovered the difference between Bubbling and Capturing on DOM events, using javascript. Now I understand how it's supposed to work, but I've found a weird situation and I would like to know why is that happening.

According to Quirks mode, the event propagation starts with capturing on the outer div, reaches the bottom and then bubbles up to the top. The problem was when I started doing some tests.

On this first one, everything works as expected:

<div id="out">
    <div id="in">
        Click This!!
    </div>
</div>
<script type="text/javascript">
    document.getElementById('out').addEventListener('click', function(){
        alert('capture out');
    }, true);
    document.getElementById('in').addEventListener('click', function(){
        alert('capture in');
    }, true);
    document.getElementById('out').addEventListener('click', function(){
        alert('bubble out');
    }, false);
    document.getElementById('in').addEventListener('click', function(){
        alert('bubble in');
    }, false);
</script>

If you click the text, the alerts go 'capture out', 'capture in', 'bubble in' and 'bubble out'. The problem is using the following code:

<div id="out">
    <div id="in">
        Click This!!
    </div>
</div>
<script type="text/javascript">
    document.getElementById('out').addEventListener('click', function(){
        alert('bubble out');
    }, false);
    document.getElementById('in').addEventListener('click', function(){
        alert('bubble in');
    }, false);
    document.getElementById('out').addEventListener('click', function(){
        alert('capture out');
    }, true);
    document.getElementById('in').addEventListener('click', function(){
        alert('capture in');
    }, true);
</script>

In this case the alerts go 'capture out', 'bubble in', 'capture in' and 'bubble out'. If you notice, the only difference is that on the second one the bubbling is assigned first, but I don't think that should make any difference.

I've tried this with Firefox and Chrome, and the results are the same (I understand that internet explorer doesn't handle capturing).

Share Improve this question edited Jan 29, 2012 at 16:54 Nickolay 32.1k13 gold badges110 silver badges194 bronze badges asked Jan 28, 2012 at 11:08 Noel De MartinNoel De Martin 2,8575 gold badges30 silver badges39 bronze badges 1
  • thanx for this to-the-point question. made my Idea clear about capturing and bubbling. – Rajesh Paul Commented Oct 29, 2013 at 5:47
Add a ment  | 

1 Answer 1

Reset to default 10

quirksmode simplified the model a little. Events in fact go through up to three phases: capturing, at target, and bubbling.

If you log the event.eventPhase like this:

document.getElementById('out').addEventListener('click', function(e){
    console.log(e.eventPhase + " : " + e.target.id + " : bubbling");
}, false);

... you'll see that the 'bubble in' and 'capture in' listeners fire during the AT_TARGET phase. Event listeners invoked for the same element during the same phase are invoked in the registration order.

Post a comment

comment list (0)

  1. No comments so far