在另一个div中的一个div之后获取内容 [英] Get content after a div within another div

查看:231
本文介绍了在另一个div中的一个div之后获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div class="mother">
   <div class="son">Content</div>
   <h3></h3>
   text
   <div class="anotherSon">Content</div>
</div>

因此,在此示例中,我想将所有之后的都保存在mother-div中的"son" -div中(并将其保存在变量中).内容可以是任何类型,可以是文本或元素.在jquery中最简单的方法是什么?

So in this example I would like to get everything after the "son"-div within the mother-div (and save it in a variable). The content can be of any type, it can be text or elements. What's the easiest way to do this in jquery?

推荐答案

如果要包括文本节点,则需要使用contents()并弄清相对于其父级的.son索引:

If you want to include text nodes, you will need to use contents() and figure out the index of .son in relation to its parent:

var $contents = $(".mother").contents();
var sonIdx = $contents.index($(".son"));
var elements = $contents.filter(function(i) {
    return i > sonIdx;
}).get();

这是一个小提琴,它可以演示.

这篇关于在另一个div中的一个div之后获取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆