选择具有空格的元素

时间:2014-10-19 11:42:52

标签: javascript jquery html css

单击时我有一个按钮,它隐藏了所有带有空td元素的表行,但是当我的页面呈现时,我得到了一些td元素,当我在firebug中打开它们时看起来像这样:

enter image description here

如何选择包含这些不可见空格的字段?由于某种原因,标签的行为类似于块元素示例:

<b></b> Will produce as is

然而

    <b>    
........// This happens because I have if/else statements  
........@if($value == '')..
........@endif..
....</b>

所有......变成白色空间。

不会像预期的那样转向<b></b>。我可以使用CSS修复此问题,还是可以使用jQuery选择这些字段。

element:empty无法工作。

2 个答案:

答案 0 :(得分:4)

选择文本只是空格的<td>元素:

$('td').filter(function(){
    return $(this).text().trim().length === 0;
// obviously the CSS call is simply an example of chaining, adjust as appropriate
}).css('background-color','#ffa');
但是,我会建议在创建时从HTML中删除无关的 - 显然是不需要的 - 空格 - 这样就可以让你简单地使用:

$('td:empty').css('background-color','#ffa');

参考文献:

答案 1 :(得分:0)

你可以像这样使用smth:

$('div,span,b,p').each(function() { //here you can specify necessary tags
         if (jQuery.trim($(this).text()) == "") {
                   //Do Something
         }
});