这是我当前的代码:
$(".close_btn").click(function(e) {
$(".box1").fadeOut(100);
$(".box2").fadeOut(100);
});
如您所见,目前,当您单击 .close_btn
时,两者.box1
和.box2
将被隐藏。现在我想使用$(this)
和.closest()
仅隐藏一个框(单击的 .close_btn
的父级,有两个 .close_btn
)。我的意思是我想要这样的东西:
$(".close_btn").click(function(e) {
$(this).closest(".box1" OR ".box2").fadeOut(100);
});
这样做可能吗?
最佳答案
您可以仅使用逗号分隔多个选择器:
$(".close_btn").click(function(e) {
$(this).closest(".box2, .box1").fadeOut(100);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box1">
<button class="close_btn">Close</button>
</div>
<div class="box2">
<button class="close_btn">Close</button>
</div>
关于javascript - 如何在 jQuery 的选择器中定义类似 OR 的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005220/