Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 是否可以抑制父项';s:活动伪类?_Jquery_Html_Css - Fatal编程技术网

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');
});