在我的网页上,我为页面上呈现的每条记录输出 jQuery,如下所示:
if ($.trim($('#attachment<%# Eval("Id")%> .content').html()) == '') {
$('#attachmentClick<%# Eval("Id")%>').hide();
}
注意元素 ID 上有服务器绑定(bind),以确保每条记录都经过 jQuery 处理。有没有办法通过嵌入条件语句在页面上只执行一次,例如“for all $(this.ID + '.attachmentsClick')
, hide only if $( '.attachments.content').html()
trim 后是空白"?
最佳答案
你可以使用 jQuery's filter function将潜在元素集减少为内容为空的元素,然后隐藏它们:
$('.attachmentsClick').filter(function(){
return $.trim($(this).find('.content').html()) == '';
}).hide();
这假设您将“attachmentsClick”类赋予所有行元素。它不需要行元素的 ID。只需在您的内容加载后运行此命令,它就会隐藏所有带有“attachmentsClick”类的元素,这些元素的子元素带有“content”类, trim 后为空。
您的 HTML 可能类似于:
<div class="attachmentsClick"><!-- this one will be hidden -->
<div class="content"></div>
</div>
<div class="attachmentsClick"><!-- this one will be hidden too -->
<div class="content"> </div>
</div>
<div class="attachmentsClick"><!-- this one will NOT be hidden -->
<div class="content"><p>some content<p></div>
</div>
关于javascript - 将多个 jQuery 条件语句合并为一个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7230353/