Javascript 如果所有子DIV都隐藏,如何隐藏父DIV(显示:无)

Javascript 如果所有子DIV都隐藏,如何隐藏父DIV(显示:无),javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有这个HTML块 <div class="abc"> <div class="xyz" style="display: none;">{.......}</div> <div class="xyz" style="display: none;">{.......}</div> <div class="xyz" style="display: none;">{.......}</div> <div clas

我有这个HTML块

<div class="abc">
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
</div>

{.......}
{.......}
{.......}
{.......}
{.......}
所以,如果所有的子div都被隐藏,那么我想在一些JS操作中隐藏父div(.ABC)

感谢使用伪类选择器并基于可见div的计数显示。可以使用方法根据布尔值切换可见性

$('.abc')。切换($('.xyz:visible')。长度!=0)

{.......}
{.......}
{.......}
{.......}
{.......}
试试这个:

$(document).ready(function(){
  var children = $(".abc").find($('.xyz'));
  if($(children).is(":hidden")){
    $(".abc").hide();
  }
  else{
    $(".abc").show();
  }

});
这里是

试试这个:

$('#btn_hide').click(function(){
var status = 0;
$('.xyz').each(function(){
 if($(this).is(":visible")){
   alert('Do not hide abc');
   status = 0;
 } else 
 {
  status = 1;
 }
})
if(status == 1){
$('.abc').hide();
} else
{
$('.abc').show();
}
})

<div class="abc">asdas
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
</div>

<input type="button" id="btn_hide" value="Hide">
$('btn_hide')。单击(函数(){
var状态=0;
$('.xyz')。每个(函数(){
如果($(this).is(“:可见”)){
警报(“不要隐藏abc”);
状态=0;
}否则
{
状态=1;
}
})
如果(状态==1){
$('.abc').hide();
}否则
{
$('.abc').show();
}
})
阿斯达斯
{.......}
{.......}
{.......}
{.......}
{.......}
工作演示:


您可以使用JavaScript进行检查,如果带有
xyz
类的所有元素的显示样式为:none,则隐藏父元素,否则不执行任何操作

if($('.xyz').css('display')=='none'){
$('.abc').hide();
}
.abc{
宽度:100px;
高度100px;
背景:红色;
}

ABC部
{.......}
{.......}
{.......}
{.......}
{.......}

遍历所有子dom元素,检查显示样式并更新状态

$(函数(){
var=true;
$(“按钮”)。单击(函数(){
$('.xyz')。每个(函数(索引,项){
log($(item.css(“display”));
如果($(项).css(“显示”)!=“无”){
hid=假;
}
}).promise().done(函数()){
if(hid==true){
console.log(“真”);
$('.abc').hide();
}
});
})  
})

{.......}
{.......}
{.......}
{.......}
{.......}

单击
一种方法,在这里我们使用一个
单击
事件,因为您没有给出关于如何隐藏子元素的信息,如下所示:

function toggleOnHiddenChildren() {

  // here we set the the CSS 'display' property
  // via the HTMLElement.style interface,
  // using a conditional ('ternary') operator:
  this.style.display = 

    // here we use Array.prototype.slice(), along
    // with Function.prototype.call(), to convert
    // the NodeList of the 'this.children' into an
    // Array, and then use Array.prototype.every()
    // to iterate over that Array in order to test
    // whether all elements match the supplied
    // test:
    Array.prototype.slice.call(this.children).every(function(child) {

    // we're using window.getComputedStyle() in order
    // to obtain the CSS display property-value regardless
    // of whether the style was set as an inline style
    // (as it would be if directly applied by JavaScript)
    // or via a stylesheet (as it would be if the style was
    // applied via the use of a class-name).
    // if the display property-value is 'none' (the element
    // is hidden) then this returns Boolean true, if all elements
    // return true then the Array.prototype.every() method
    // also returns true, which then causes the display of
    // the 'this' element to be set to 'none', otherwise to
    // 'block':
    return window.getComputedStyle(child, null).display === 'none';
  }) ? 'none' : 'block';
}

// creating an Array of the <div> elements with the class-
// name of 'abc':
var abcElements = Array.prototype.slice.call(
    document.querySelectorAll('div.abc')
);

// iterating over the Array of elements using
// Array.prototype.forEach():
abcElements.forEach(function(abc){
  // 'abc' : a reference to the current element of the
  //         Array of elements over which we're iterating.

  // here we add an event-listener for the 'click' event
  // which calls the named function as the event-handler
  // (note the deliberate lack of parentheses):
  abc.addEventListener('click', toggleOnHiddenChildren);
});
.abc{
边框:2倍实心#000;
高度:2米;
背景色:#f90;
}

{.......}
{.......}
{.......}
{.......}
{.......}

向我们展示您迄今为止所做的尝试—您是否使用javascript动态隐藏子对象?在堆栈溢出问题上,最好添加一个解释,说明您的解决方案为什么应该有效。欲了解更多信息,请阅读此
$('#btn_hide').click(function(){
var status = 0;
$('.xyz').each(function(){
 if($(this).is(":visible")){
   alert('Do not hide abc');
   status = 0;
 } else 
 {
  status = 1;
 }
})
if(status == 1){
$('.abc').hide();
} else
{
$('.abc').show();
}
})

<div class="abc">asdas
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
<div class="xyz" style="display: none;">{.......}</div>
</div>

<input type="button" id="btn_hide" value="Hide">
function toggleOnHiddenChildren() {

  // here we set the the CSS 'display' property
  // via the HTMLElement.style interface,
  // using a conditional ('ternary') operator:
  this.style.display = 

    // here we use Array.prototype.slice(), along
    // with Function.prototype.call(), to convert
    // the NodeList of the 'this.children' into an
    // Array, and then use Array.prototype.every()
    // to iterate over that Array in order to test
    // whether all elements match the supplied
    // test:
    Array.prototype.slice.call(this.children).every(function(child) {

    // we're using window.getComputedStyle() in order
    // to obtain the CSS display property-value regardless
    // of whether the style was set as an inline style
    // (as it would be if directly applied by JavaScript)
    // or via a stylesheet (as it would be if the style was
    // applied via the use of a class-name).
    // if the display property-value is 'none' (the element
    // is hidden) then this returns Boolean true, if all elements
    // return true then the Array.prototype.every() method
    // also returns true, which then causes the display of
    // the 'this' element to be set to 'none', otherwise to
    // 'block':
    return window.getComputedStyle(child, null).display === 'none';
  }) ? 'none' : 'block';
}

// creating an Array of the <div> elements with the class-
// name of 'abc':
var abcElements = Array.prototype.slice.call(
    document.querySelectorAll('div.abc')
);

// iterating over the Array of elements using
// Array.prototype.forEach():
abcElements.forEach(function(abc){
  // 'abc' : a reference to the current element of the
  //         Array of elements over which we're iterating.

  // here we add an event-listener for the 'click' event
  // which calls the named function as the event-handler
  // (note the deliberate lack of parentheses):
  abc.addEventListener('click', toggleOnHiddenChildren);
});