Code Monkey home page Code Monkey logo

Comments (9)

longjinxiang avatar longjinxiang commented on August 22, 2024 5

如果该 DOM 是目标元素,则按事件绑定顺序执行,不区分冒泡/捕获 <<< 这句话有问题,测试以下代码,都是先执行捕获,再执行冒泡,与事件绑定顺序无关

window.addEventListener('click', () => {console.log('aa')})
window.addEventListener('click', () => {console.log('bbbbbbb')}, true)

from fe-interview.

QQhuan avatar QQhuan commented on August 22, 2024 2

老哥,经过实践,触发目标元素的点击事件,不论绑定捕获冒泡的顺序如何更改,回调的顺序都是先捕获再冒泡!!!火狐谷歌都是...
如果是我理解有误麻烦指正,如果是参考分析有误还请订正。

from fe-interview.

specialgirlgotoheaven avatar specialgirlgotoheaven commented on August 22, 2024

chrome,firefox都支持两种事件,捕获和冒泡,IE仅支持冒泡

from fe-interview.

Zenquan avatar Zenquan commented on August 22, 2024
 <div id="btn">点击
    <div id="btn2" style="margin: 100px;">
      点击2
    </div>
  </div>
  <script>
    const btn = document.getElementById('btn')
    const btn2 = document.getElementById('btn')
    btn.addEventListener('click', () => {console.log('aa')})
    btn.addEventListener('click', () => {console.log('bb')}, true)
    
    btn2.addEventListener('click', () => {console.log('cc')})
    btn2.addEventListener('click', () => {console.log('dd')}, true)
  </script>

在chrome的结果都是

"bb"
"dd"
"aa"
"cc"

from fe-interview.

bigbigfan avatar bigbigfan commented on August 22, 2024

公众号好像不维护了。但是还是帮忙留言chrome的实际行为是「该场景下捕获总是在冒泡之前触发」

from fe-interview.

whenTheMorningDark avatar whenTheMorningDark commented on August 22, 2024

如果目标DOM元素有事件,则先事件,后捕获,然后在冒泡。

from fe-interview.

YuArtian avatar YuArtian commented on August 22, 2024
 <div id="btn">点击
    <div id="btn2" style="margin: 100px;">
      点击2
    </div>
  </div>
  <script>
    const btn = document.getElementById('btn')
    const btn2 = document.getElementById('btn')
    btn.addEventListener('click', () => {console.log('aa')})
    btn.addEventListener('click', () => {console.log('bb')}, true)
    
    btn2.addEventListener('click', () => {console.log('cc')})
    btn2.addEventListener('click', () => {console.log('dd')}, true)
  </script>

在chrome的结果都是

"bb"
"dd"
"aa"
"cc"

哈哈哈哈哈哈哈哈哈

from fe-interview.

robert88 avatar robert88 commented on August 22, 2024
const btn2 = document.getElementById('btn') 错误了

from fe-interview.

houchaowei avatar houchaowei commented on August 22, 2024

chrome,firefox都支持两种事件,捕获和冒泡,IE仅支持冒泡

我觉得这个得加上浏览器的兼容性问题,脱离兼容性说这个,有点缺乏完整性,很多回答是ie之外的浏览器,确实是不论绑定的事件的先后顺序,先捕获后冒泡,这个可以结合react中实现的合成事件来说或许更完整些,react的合成事件就是基于兼容性,为了磨平浏览器的差异,实现了一套类似冒泡阶段执行事件的机制,在合成事件中,只在冒泡阶段执行一次。

from fe-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.