使用jQuery时,如何仅从DIV包含带有子元素的文本中获取文本? [英] How to get text only from the DIV when it has child elements with text using jQuery?

查看:71
本文介绍了使用jQuery时,如何仅从DIV包含带有子元素的文本中获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用jQuery的子元素包含文本时,如何从父DIV中获取文本?

How to get the text from a parent DIV when it has child elements with text using jQuery?

<div id='div_1'>
  I am a DIV
  <span>
    I am a SPAN
  </span> 
</div>

如果我要使用$('#div_1').text(),它将显示我是DIV,我是SPAN".如何仅显示文本我是DIV,来自" div_1 ?非常感谢您的帮助.

If I was to use $('#div_1').text(),it will give me 'I am a DIV I am a SPAN'.How do I get just the text 'I am a DIV from' div_1? Any help much appreciated,thank you.

推荐答案

尝试使用:

$('#div_1').clone().children().remove().end().text();

这篇关于使用jQuery时,如何仅从DIV包含带有子元素的文本中获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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