【问题标题】:How to select elements those have certain two ancestors?如何选择具有某些两个祖先的元素?
【发布时间】:2019-06-22 18:20:39
【问题描述】:

我想选择所有具有

的元素
  1. X 类的祖先,并且
  2. id 为 Y 的祖先。

出于某种原因。我需要使用jquery的'on'方法 所以请告诉我一个我可以用'on'方法使用的字符串

【问题讨论】:

标签: jquery


【解决方案1】:

jsfiddle 示例

你的html

<div class="container">

  <button class="check">
     check
  </button>

  <div id="y">
    <div class="x">
      test1
    </div>
  </div>

  <div id="p">
    <div class="x">
      test2
    </div>
  </div>

  <div id="y">
    <div class="p">
      test3
    </div>
  </div>

  <div id="y">
    <div class="x">
      test4
    </div>
  </div>

</div>

你的 JavaScript 函数

$(document).ready(function() {
  $(".check").click(function() {
    $('[id=y]').each(function() {
      if ($(this).find(".x").length > 0) {
        alert($(this).text());
      }
    });
  });
});

结果
测试1
测试4

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 1970-01-01
    • 2015-01-25
    • 2023-03-26
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多