Roh*_*hra 7 html jquery trim hide html-lists
<li>如果它们是空的或者如果有任何空白,我想隐藏所有<li>.
我是这样做的:
$("li:empty").filter(function(i,v){return $.trim($(v).text()).length == 0;}).css('display', 'none');
Run Code Online (Sandbox Code Playgroud)
这是错误的语法吗?
如果我创建一个类使空<li>隐形,它工作正常.像这样:
$("li[class='hideLi']").filter(function(i,v){return $.trim($(v).text()).length == 0;}).css('display', 'none');
Run Code Online (Sandbox Code Playgroud)
但我不知道哪个<li>是空的.
任何人都可以帮助我PLZ.display=none如果<li>是空的我想做.
这是我的代码: -
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("td:empty").css('display', 'none');
$("div:empty").css('display', 'none');
$(!$.trim('li:empty')).hide();
$("li[class='hideLi']").filter(function(i,v){return $.trim($(v).text()).length == 0;}).css('display', 'none');
});
</script>
<ul>
<li style="border:red solid 1px ">HI</li>
<li style="border:green solid 1px "> </li>
<li style="border:blue solid 1px " class="hideLi"> </li>
</ul>
Run Code Online (Sandbox Code Playgroud)
谢谢,
你可以使用.hide()
如果你有空格使这个工作,你可以使用jQuery.trim()
$("li").each(function() {
if(!$.trim($(this).html())) {
$(this).hide();
}
});
Run Code Online (Sandbox Code Playgroud)
工作示例:小提琴示例
$('li').filter(function(){
return $.trim($(this).html()) == '';
}).hide()
Run Code Online (Sandbox Code Playgroud)