如果子div不包含标记,如何隐藏父div?
如果子div(即.post-title)不包含标签,我试图隐藏父div(即#post)。 有关如何使用我修改过的代码正确完成此操作的任何见解?
$('.post-title').not($('a').hide($("#post"));
首先:不要多次使用ID。 每个ID都应该是唯一的:)
其次: $('.post').not( ':has(".post-title a")' ).addClass('hide')
注意:复制
id
是犯罪行为。 😉
通过将所有id
dupes更改为class
,这应该有效:
$(function () { $(".post-title").filter(function () { return $(this).find("a").length == 0; }).hide(); });
Hide This
你可以简单地使用jQuery的.filter
像这样:
$('.post-title').filter(function(i) { return !$(this).find('a').length }).hide();
见下面的例子
只需使用jQuery的切换,使用布尔值来检查子项的长度:
/* Loop Through Parents */ $('#post').each(function() { $(this).toggle($(this).children('a').length > 0); });