我有以下情况
<div id="parent">
<div id="child"></div>
</div>
还有一些CSS
div:active { background-color: green }
有没有更好的方法,当 #child
被点击时,只有 #child
被激活 。我试过了
$('#child').click(function (e) {
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
});
但无论我做什么,#parent
也会激活
。请注意,我无法更改 CSS!
无论如何,我看到的唯一解决方案是添加一个 .active
类而不是使用 :active
但我希望它可以通过事件传播以某种方式完成。
最佳答案
来自CSS spec :
The parent of an element that matches
:active
also matches:active
.
所以不,似乎没有办法让父级不匹配:active
。您要么必须使选择器更具体,要么不使用 :active
。
关于javascript - 阻止事件冒泡到父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30738903/