点击.btn时,我需要关闭(删除类.active)。
可能存在冲突,因为还要点击.item。
它只适用于点击其他.item。
$(".item").on("click", function() {
$(".item").removeClass("active");
$(this).addClass("active");
});
//$(".btn").on("click", function(){
// $(".item").removeClass("active");
//});
$(".btn").on("click", function() {
$(this).parent(".item").removeClass("active");
});

* {
box-sizing: border-box;
transition: 0.5s;
margin: 0;
padding: 0;
}
.clear {
clear: both;
height: 0;
}
.wrap {
padding: 20px;
background: #eee;
color: #fff;
}
.btn {
display: block;
line-height: 100px;
position: relative;
z-index: 100;
font-size: 14px;
text-align: center;
cursor: pointer;
}
.btn:hover {
background: #f00;
}
.item {
position: relative;
float: left;
margin: 2px;
width: 100px;
height: 100px;
text-align: center;
background: purple;
overflow: hidden;
}
.item.active {
overflow: visible;
width: 200px;
height: 200px;
}
.large {
width: 100px;
height: 100px;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
.active .large {
width: 200px;
height: 200px;
background: red;
opacity: 1;
z-index: 10;
}
.close {
position: absolute;
opacity: 0;
right: 8px;
bottom: 8px;
width: 16px;
height: 16px;
text-align: center;
background: black;
cursor: pointer;
font-family: Arial;
font-size: 11px;
}
.active .close {
opacity: 1;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
<div class="item">
<h2 class="btn">item 1</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 2</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 3</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 4</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 5</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 6</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 7</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="clear"></div>
</div>
&#13;
答案 0 :(得分:1)
防止事件冒泡到父“item”。使用event.stopPropagation
:
$(".btn").on("click", function(e){
$(this).parent(".item").removeClass("active");
e.stopPropagation();
});
https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation
当然,按照您的设计方式,父母将永远不会收到点击次数。
答案 1 :(得分:1)
您需要阻止其中一个按钮中的默认操作,否则将执行两个操作(开启者和关闭操作)。对任何jQuery事件返回false
将阻止默认操作并停止事件的传播。
检查我的示例,我将关闭按钮操作更改为span.close
元素:
$(".item").on("click", function(){
$(".item").not($(this)).removeClass("active");
$(this).addClass("active");
});
$(".close").on("click", function(){
$(this).closest(".item.active").removeClass("active");
return false;
});
*{box-sizing:border-box; transition:0.5s; margin:0; padding:0;}
.clear{clear:both; height:0;}
.wrap{padding:20px; background:#eee; color:#fff;}
.btn{display:block; line-height:100px; position:relative; z-index:100; font-size:14px; text-align:center; cursor:pointer;}
.btn:hover{background:#f00;}
.item{position:relative; float:left; margin:2px; width:100px; height:100px; text-align:center; background:purple; overflow:hidden;}
.item.active{overflow:visible; width:200px; height:200px;}
.large{width:100px; height:100px; position:absolute; left:0; top:0; opacity:0;}
.active .large{width:200px; height:200px; background:red; opacity:1; z-index:10;}
.close{position:absolute; opacity:0; right:8px; bottom:8px; width:16px; height:16px; text-align:center; background:black; cursor:pointer; font-family:Arial; font-size:11px;}
.active .close{opacity:1;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrap">
<div class="item">
<h2 class="btn">item 1</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 2</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 3</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 4</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 5</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 6</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="item">
<h2 class="btn">item 7</h2>
<div class="large">
<span class="close">x</span>
</div>
</div>
<div class="clear"></div>
</div>
答案 2 :(得分:0)
切换方法怎么样?
$(".item").on("click", function(){
$(".item").removeClass("active");
$(this).toggleClass("active");
});