Javascript - event.stopPropagation() 不能在 BS4 Accordion 上从 parent 到 child 工作

标签 javascript bootstrap-accordion

我正在使用相互嵌套的 BS4 Accordion 。

我遇到过父到子和子到父的事件冒泡问题,我已经使用 event.stopPropagation() 解决了从子到父的问题,但是从父到子的事件冒泡没有停止,为什么?

这是代码

$('.card > .collapse').on('shown.bs.collapse', function(e){
    e.preventDefault();  
    e.stopPropagation();
      $(this).parent().find(".flaticon-down-arrow").removeClass("flaticon-down-arrow").addClass("flaticon-right-arrow");

  }).on('hidden.bs.collapse', function(e){
    e.preventDefault();  
    e.stopPropagation();
    $(this).parent().find(".flaticon-right-arrow").removeClass("flaticon-right-arrow").addClass("flaticon-down-arrow");

})

Here is my codepen link

注意: - 检查类的变化

最佳答案

事件冒泡只会沿着 DOM 树向上(从子节点到父节点,再到祖父节点,等等)。它不会向下 DOM 树。因此,父级上的任何事件都不会冒泡到其子级。

documentation指出在事件冒泡期间(对于点击事件):

The browser checks to see if the element that was actually clicked on has an onclick event handler registered on it in the bubbling phase, and runs it if so. Then it moves on to the next immediate ancestor element and does the same thing, then the next one, and so on until it reaches the element.

关于Javascript - event.stopPropagation() 不能在 BS4 Accordion 上从 parent 到 child 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51800599/

相关文章:

javascript - Bootstrap 模式启动递增时间

jquery - 构建自举 Accordion 并想向整个面板添加一个类

javascript - 折叠时更改 bootstrap-accordion 的超时

Angular 12 Bootstrap 5 ngFor Accordion 不关闭

javascript - java 中的 selenium 无法点击 href ="javascript:void(0);"

javascript - 默认情况下,无法在 foreach 循环中保持第一个 Accordion 打开

javascript - 单击并激活 <a> 元素 JavaScript

javascript - 在html5中一个接一个地播放视频

javascript - 将字段插入子文档数组中。意外的行为

javascript - 用于移动设备的 Html 导航栏菜单