我正在使用相互嵌套的 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");
})
注意: - 检查类的变化
最佳答案
事件冒泡只会沿着 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/