【问题标题】:Find all elements with same class but exclude the clicked element? [duplicate]查找具有相同类但排除单击的元素的所有元素? [复制]
【发布时间】:2018-09-20 14:42:52
【问题描述】:

我有这个 html:

<div class="parent">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
</div>

在点击的情况下,我需要触发一些操作。问题是我需要排除点击的元素。假设我单击第一个 .child ,然后操作仅应用于第二个和第三个 .child 元素。

  $('.child').on("click", function() {
    $(".parent").find(".child").DO_SOMETHING();
  });

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    像这样使用 jQuery not()

      $('.child').on("click", function() {
        $(".parent").find(".child").not($(this));
      });
    

    示例

    $('.child').on("click", function() {
        $(".parent").find(".child").css("color", "black").not($(this)).css("color", "red");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <div class="parent">
        <div class="child">One</div>
        <div class="child">Two</div>
        <div class="child">Three</div>
    </div>

    【讨论】:

    • 你不需要创建一个完整的 jQuery 对象,只需 not(this) 就可以了。
    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 2018-10-20
    • 2017-03-22
    • 2014-09-05
    • 1970-01-01
    • 2013-07-03
    • 2012-04-29
    • 1970-01-01
    相关资源
    最近更新 更多