使用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();