如果所有子div都被隐藏,如何隐藏父div?

时间:2018-08-14 07:18:51

标签: javascript html css

我正在像这样动态创建div。

<div class='parent'>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
</div>

<div class='parent'>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
</div>

<div class='parent'>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
  <div class='child'></div>
</div>

我正在使用此脚本来过滤结果...

$(document).ready(function(){
  $("#search").on("keyup", function()
  {
    var value = $(this).val().toLowerCase();
    $(".child").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
    });
  });
});

这很好用,我想实现的是,假设搜索值在first div的任何子级中都不匹配,那么它也应该隐藏父div,即first div。

3 个答案:

答案 0 :(得分:2)

通过父母围成一圈,检查是否所有孩子都被隐藏了

$('.parent').show();
$('.parent').each(function() {
  if (!$(this).find('.child:visible').length) {
    $(this).hide();
  } else {
    $(this).show();
  }
})

答案 1 :(得分:0)

尝试

elementToBeClickable

答案 2 :(得分:0)

除了您可以在其他地方使用thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', libcore/option.rs:345:21 stack backtrace: 0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49 1: std::sys_common::backtrace::print at libstd/sys_common/backtrace.rs:71 at libstd/sys_common/backtrace.rs:59 2: std::panicking::default_hook::{{closure}} at libstd/panicking.rs:211 3: std::panicking::default_hook at libstd/panicking.rs:227 4: std::panicking::rust_panic_with_hook at libstd/panicking.rs:511 5: std::panicking::continue_panic_fmt at libstd/panicking.rs:426 6: rust_begin_unwind at libstd/panicking.rs:337 7: core::panicking::panic_fmt at libcore/panicking.rs:92 8: core::panicking::panic at libcore/panicking.rs:53 9: rust_practices::main 10: std::rt::lang_start::{{closure}} 11: std::panicking::try::do_call at libstd/rt.rs:59 at libstd/panicking.rs:310 12: __rust_maybe_catch_panic at libpanic_unwind/lib.rs:105 13: std::rt::lang_start_internal at libstd/panicking.rs:289 at libstd/panic.rs:392 at libstd/rt.rs:58 14: main 15: __libc_start_main 16: _start 之外,这一点与上面类似

toggle_visibility()