从Div减去子元素的jQuery抓取文本 [英] JQuery Grab Text From Div minus child element

查看:135
本文介绍了从Div减去子元素的jQuery抓取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简便的方法可以在不获取任何子元素的情况下从该div中获取文本?

Is there an easy way to grab the text from this div with out getting any child elements?

<div id="someselector">
   <strong>Title Text Unwanted</strong> This is the text I need
</div> 

我知道我可能可以在强字符串上做一个子字符串.我只是想知道是否有一个功能可以自动在jQuery中为我做 像:

I know I probably could do a substring on the strong. I was just wondering if there was a function that could automatically do it for me in jQuery like:

 $('#someselector').html().not('strong').text()

推荐答案

基本上可以归结为:

$("#someselector").clone().children().remove().end().text();

首先,克隆元素,获取其子元素,将其删除,然后获取克隆的文本.

First, you clone the element, get its children, remove them and then get the text of the clone.

这篇关于从Div减去子元素的jQuery抓取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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