【问题标题】:How can I define something like OR in jQuery's selector? [duplicate]如何在 jQuery 的选择器中定义类似 OR 的东西? [复制]
【发布时间】:2017-06-19 16:50:30
【问题描述】:

这是我当前的代码:

$(".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);
});

这样做可能吗?

【问题讨论】:

  • $(this).closest(".box1, .box2") ?
  • @Karl-AndréGagnon 是的 .. 很棒。谢谢

标签: javascript jquery html jquery-selectors


【解决方案1】:

您可以只使用逗号来分隔多个选择器:

$(".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>

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 2011-05-13
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2015-11-26
    • 1970-01-01
    相关资源
    最近更新 更多