Jquery 是否可以抑制父项';s:活动伪类?
我有以下标记:Jquery 是否可以抑制父项';s:活动伪类?,jquery,html,css,Jquery,Html,Css,我有以下标记: <div class="parent"> I should change my color to green when clicked <div class="element">I should do nothing when clicked</div> </div> 单击.element时,是否有任何方法防止触发.parent的:active伪类?当点击.element时,尝试了e.stopPropogation(),但
<div class="parent">
I should change my color to green when clicked
<div class="element">I should do nothing when clicked</div>
</div>
单击.element
时,是否有任何方法防止触发.parent
的:active
伪类?当点击.element
时,尝试了e.stopPropogation()
,但没有成功。
您可以只使用jQuery而不使用
:active
,如下所示
可能与js有关
$(document).ready(function(){
$(".parent").click(function(){
$(this).css("background","green")
});
$(".parent .element").click(function(e) {
return false;
});
});
还有css
.parent {
width:200px;
height:200px;
background: red;
color: #fff;
}
.parent:active {
}
.element {
background:blue;
}
HTML是相同的门把手的替代解决方案是使用
事件。目标
:
$('.parent').mousedown(function(e){
if ($(e.target).is(this))
$('.parent').css('background', 'green');
}).mouseup(function(e){
if ($(e.target).is(this))
$('.parent').css('background', 'red');
});
.如何在父级上引入一个额外的类,例如
允许活动
<div class="parent allow-active">
这意味着只有当div同时具有父类
和允许活动类时,颜色才会改变
然后使用jQuery切换允许活动
类
$('.element').mousedown(function(e){
$('.parent').removeClass('allow-active');
}).mouseup(function(e){
$('.parent').addClass('allow-active');
});
我想jQuery.stopPropogation()
对css渲染没有影响,只是对JavaScript事件没有影响。谢谢,这已经接近了我现在的做法,直到我没有更好的解决方案为止。
<div class="parent allow-active">
.parent.allow-active:active {
background:green;
}
$('.element').mousedown(function(e){
$('.parent').removeClass('allow-active');
}).mouseup(function(e){
$('.parent').addClass('allow-active');
});