jquery - 在特定元素后查找 Div

标签 jquery jquery-selectors element

我能够使用 .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/

相关文章:

javascript - 尝试使用 jQuery 创建菜单,但我的代码无法正常工作

python - Numpy ValueError : setting an array element with a sequence. 此消息可能会在不存在序列的情况下出现?

javascript - 将 TypeAhead 与 jQuery 和 Bootstrap 2.1 结合使用

仅 jQuery 移动滑动内容

jquery - focus() 后 contenteditable 中的额外换行符

javascript - 通过 jQuery 获取整个 HTML 文档,无论顶级元素如何

javascript - jQuery ID 选择器

jquery - 如何使用jquery选择另一个特殊标签后的特殊标签

matlab - 如何选择给定(非常大)矩阵的单个元素,查看它们是否在特定范围内并在 Matlab 中更改它们?

javascript - 一张 Canvas ,两个 dom 元素?