代码之家  ›  专栏  ›  技术社区  ›  nrweb

使用JQuery在页面的第一个元素中选择第一个出现的子元素?

  •  0
  • nrweb  · 技术社区  · 7 年前

    我想使用 gt() a中的选择器 .load() 方法这能做到吗?

    目前,我正在寻求一种解决方案,使我能够应用 .children(':gt(0)') 选择器之后 .entry:first 被调用。

    以下是我目前的进展:

    $("#embed-content").load("https://URL.com/page/ .entry:first .children(':gt(0)')", function(data) {});

    注意,加载源中的第一个元素并不总是 div ,并且并不总是包含相同的id或类,这些方面会有所不同。

    1 回复  |  直到 7 年前
        1
  •  1
  •   T.J. Crowder    7 年前

    .entry:first .children(':gt(0)') 可能是 .entry:first > :gt(0) . 这个 > 是直接子组合子,当然 :gt(0) 是“大于索引0”。

    但我认为这不是你想要的。您在评论中说:

    我正在尝试选择第一个 .entry 加载的页面源的div。

    :gt(0) 跳过 第一个这样的元素,而不是选择它。要选择所需内容,请执行以下操作: .entry:first > :first :

    $(".entry:first > :first").css({
      color: "green",
      fontWeight: "bold"
    });
    <div class="entry">
      <span>first child of first .entry</span>
      <span>second child of first .entry</span>
    </div>
    <div class="entry">
      <span>first child of second .entry</span>
      <span>second child of second .entry</span>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>