在div中获取文本 [英] get text in div

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

问题描述

可能重复:
jquery-获取没有子文本的元素的文本

Possible Duplicate:
jquery - get text for element without children text

我有一个div,其内部包含文本,并在此文本之后出现跨度:

I've a div with text inside and span after text like this:

<div>
text
<span>other text</span>
</div>

并且我只想获取div中的文本而不是跨度

and I'd like to get only text in div not in span

谢谢

推荐答案

您可以使用 .clone( ) ,例如:

You can use .clone() for that, like so:

b = $('div').clone();
b.children('span').remove();
alert( b.text() ); // alerts "text"

使用.clone(),我们可以复制div,删除跨度然后获取文本,而不会影响页面上显示的原始DOM元素.

Using .clone() we can make a copy of the div, remove the span, and then get the text, all without affecting the original DOM elements displayed on the page.

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

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