This question already has answers here:
jquery - get text for element without children text

(3个答案)


5年前关闭。




有没有一种简单的方法可以在不获取任何子元素的情况下从该div中获取文本?
<div id="someselector">
   <strong>Title Text Unwanted</strong> This is the text I need
</div>

我知道我可以对强项做一个子串。我只是想知道是否有一个功能可以自动在jQuery中为我做
喜欢:
 $('#someselector').html().not('strong').text()

最佳答案

基本上可以归结为:

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

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

10-07 19:02
查看更多