gpt4 book ai didi

jquery - 在特定元素后查找 Div

转载 作者:行者123 更新时间:2023-12-01 03:07:33 25 4
gpt4 key购买 nike

我能够使用 .class textimage-text 获取元素内的所有 HTML。这很好用。然后我需要获取以下 Div 中的 html,该 Div 没有 id 或 Class..

示例

<div class="textimage-text">
<p>Some Text goes here</p>
</div>
<br>
<div>
<p>Some additional Text goes here</p>
</div>

$.get(item.url, function (html) {

var body = $(html).find(".textimage-text").html(); // <- this works
var more= $(html).find(".textimage-text").next("div").html(); // <- this does not work

最佳答案

使用通用同级选择器~(例如:~ div查找与其同级的下一个div)

$(html).find(".textimage-text ~ div").html();

$('.textimage-text ~ div').html()

General sibling selectors

The general sibling combinator (~) separates two selectors and matches the second element only if it follows the first element (though not necessarily immediately), and both are children of the same parent element.

示例:

console.log($('body').find('.textimage-text').html());
console.log($('body').find('.textimage-text ~ div').html());

console.log($('.textimage-text').html());
console.log($('.textimage-text ~ div').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="textimage-text">
<p>Some Text goes here</p>
</div>
<br>
<div>
<p>Some additional Text goes here</p>
</div>

关于jquery - 在特定元素后查找 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45947429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com