我有以下 html 标记
<div class="title-wrap">
<span class="title">Hello World</span>
<br/>
This title is about Hello World
</div>
我想定位文本“This title is about Hello World”
但是我不知道如何在不包括跨度的情况下做到这一点?用这种格式的html可以实现吗?
我尝试过使用
var test = (jQuery('.title-wrap').text() || '' );
alert (test);
但这会返回
Hello World
This title is about Hello World
如this fiddle所示.
最佳答案
您可以使用这个:
var test = (jQuery('.title-wrap')
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text() || '' );
alert (test);
输出:
This title is about Hello World
fiddle :http://jsfiddle.net/Y6dcU/6/
关于jquery - 仅定位部分 div 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12946768/