jQuery从div中选择文本:not
我做了一个小提琴: http : //jsfiddle.net/hAzJq/
如何在div中选择文本但不在span内?
alert($("div").not('span').text());
首先使用.contents
,它将提取文本节点和HTML节点,并应用:not
selector来过滤掉span
:
alert($("div").contents(':not(span)').text());
也就是说,一种更简单的方法可能是在您想要的内容周围应用一个特殊类的不同span
,然后选择它。
使用contents()
方法从div
获取所有内容,然后过滤掉您不需要的内容。
$("div").contents().filter(function(){ return !$(this).is('span');}).text();
您还可以将选择器传递给contents()
方法。
alert($("div").contents(':not(span)').text());
工作演示 – http://jsfiddle.net/hAzJq/1/
参考文献:
-
contents()
– http://api.jquery.com/contents/ -
filter()
– http://api.jquery.com/filter/