简体   繁体   English

如果所有子项都不可见,则隐藏父级

[英]Hide parent if all children are not visible jquery

I am trying to use coffeescript to hide the title of the group with if all the li's under it are hidden, but this doesn't work. 我正在尝试使用coffeescript来隐藏该组的标题,前提是该组下的所有li都被隐藏了,但这是行不通的。 The log tells me that even when each li has display:none it is still visible. 日志告诉我,即使每个li都有display:none它仍然可见。 (ie this is selecting all lis, when it should only be selecting those that are visible) (即,这是选择所有lis,而应该只选择可见的lis)

filterGroups = () ->
  $('.group').each (idx, item) ->
    list = $(item).find("ul.bordered-list li:visible")
    console.log list.length
    if list.length == 0
      $(item).hide()
    else
      $(item).show()

The HAML structure looks like this: HAML结构如下所示:

ul.bordered-list
  li.group
    h3.group-name-header
    ul.bordered-list
      li
    ...
  ...

The arguments for .each() are .each(idx, Element) , not .each(Element, idx) ; .each()的参数是.each(idx, Element) ,而不是.each(Element, idx)

filterGroups = () ->
  $('.group').each (idx, item) -> //correct argument order
    list = $(item).find("ul.bordered-list li:visible")
    console.log list.length
    if list.length == 0
      $(item).hide()
    else
      $(item).show()

You can also use the this keyword: 您也可以使用this关键字:

$('.group').each () ->
        list = $(this).find("ul.bordered-list li:visible")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM