我正在处理一些生成的跨度,我想找出其中哪一个不包含任何文本。
标记为:
<span id="layer6">
<span class="drag col"> Some text</span>
<span class="drag col"> More text</span>
<span class="drag col"> </span>
<span class="drag col"> I also have text</span>
</span>
我可以在下面的代码中获取包含“一些文本”的内容,但不能获取空的内容:
if ($('#layer6 > span.col:contains("some text")').length > 0) {
alert ("I have text");
}
如何获取空的?我正在考虑使用.length来做这件事,但我没有做到。
发布于 2010-03-04 19:17:47
$("span.col").each(function(){
if (!$(this).text().trim().length) {
$(this).addClass("foo");
}
});
http://jsfiddle.net/cfgr9/1/
显然,不是添加一个可以随心所欲的类,而是返回对象等
更新:删除了导致js错误的最后一个分号后奇怪的隐藏字符。
发布于 2010-03-04 19:05:33
使用filter
过滤那些没有文本内容的元素:
$('#layer6 > span.col').filter(function(){
return $(this).text().trim() != "";
}).length
发布于 2012-02-05 21:05:44
$('span:empty').css('background-color','red');
https://stackoverflow.com/questions/2378620
复制相似问题