我想在 .load()
方法中使用 gt()
选择器。这能实现吗?
目前,我正在寻求一种解决方案,允许我在 .entry:first
之后应用 .children(':gt(0)')
选择器被调用。
这是我到目前为止的进展:
$("#embed-content").load("https://URL.com/page/.entry:first .children(':gt(0)')", 函数(数据) {});
请注意,加载源中的第一个元素并不总是 div
,并且不会始终包含相同的 id 或类,这些方面会有所不同。
最佳答案
.entry:first .children(':gt(0)')
将是 .entry:first > :gt(0)
。 >
是直接子组合器,当然 :gt(0)
是“大于索引 0”。
但我认为这不是你想要的。您在评论中说过:
I am trying to select the first child element within the first
.entry
div from the loaded page source.
: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>
关于javascript - 使用 JQuery 选择页面上第一个元素中第一个出现的子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48909785/