Показать блок, если все элементы другого скрыты
Как показать элемент id="show_box", если все элементы id="li_a" display="none". На данный момент если хотя бы один элемент display="block", элемент id="show_box" виден.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
}
#show_box{
display:none;
}
</style>
</head>
<body>
<center>
<div id="my_All">
<li id="li_a" style="display:block">HELLO 1</li>
<li id="li_a" style="display:none">HELLO 2</li>
<li id="li_a" style="display:none">HELLO 3</li>
</div>
<div id="show_box" >Нужно показать!!!</div>
</center>
<script>
var show_box=document.getElementById('show_box');
show_box.style.color='blue';
document.querySelectorAll('#my_All #li_a').forEach(function(el){
if (el.style.display==='none')
{show_box.style.display='block';
console.log('HHH');
}
else{
show_box.style.display='none';
}
})
</script>
</body>
</html>
|
Цитата:
|
Зачем использовать <li> там, где его не должно быть?
У него разрешенные родители только <ul> и <ol>. В других местах он будет как обычный div. Ну так и используйте div. |
Цитата:
Цитата:
|
Трудно сказать, что там нужно.
<ul> вместо родительского <div> или <div> вместо дочерних <li> |
rita,
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<style>
* {
box-sizing: border-box;
}
#show_box {
display: none;
color: #0000FF;
}
#show_box.open {
display: block;
}
</style>
</head>
<body>
<center>
<ul id="my_All">
<li class="li_a" style="display:none">HELLO 1</li>
<li class="li_a" style="display:none">HELLO 2</li>
<li class="li_a" style="display:none">HELLO 3</li>
</ul>
<div id="show_box">Нужно показать!!!</div>
</center>
<script>
let show_box = document.getElementById('show_box');
let open = [...document.querySelectorAll('#my_All .li_a')].every(li => getComputedStyle(li).display === 'none');
show_box.classList.toggle('open', open);
</script>
</body>
</html>
|
Работает. Спасибо!
|
| Часовой пояс GMT +3, время: 06:13. |