[英]How do I select all p tags and make them visible when check box is clicked?
I want all the <p>
elements to be displayed when the checkbox is checked and otherwise not displayed.我希望在选中复选框时显示所有<p>
元素,否则不显示。
function change(){ var nav = document.getElementById("navbar"); var checkBox = document.getElementById("checkbox"); if(checkBox.checked == true){ nav.style.width = "300px"; } else{ nav.style.width = "70px"; } }
nav{ display: flex; flex-direction: column; justify-content: space-between; width: 100px; height: 100%; transition: 0.3s; /* #63. change 2nd to -10px from -6px */ box-shadow: rgba(136, 165, 191, 0.26) 4px 2px 10px 0px, rgba(255, 255, 255, 0.8) -4px -2px 16px 0px; } nav a{ text-decoration: none; } nav li{ list-style-type: none; display: flex; color:black; } nav p{ display: none; } nav input{ margin: 10px; position: absolute; width: 50px; height: 50px; opacity: 0; cursor: pointer; } nav input:checked + nav p{ display: flex; }
<nav id="navbar"> <input onclick="change()" type="checkbox" name="" id="checkbox"> <ul> <a href="#"> <li> <p>Home</p> </li> </a> <a href="#"> <li> <p>Summer</p> </li> </a> </ul> </nav>
You can use document.querySelectorAll()
to select each p element and then use element.style.display
to change whether or not it is visible.您可以使用document.querySelectorAll()
到 select 每个 p 元素,然后使用element.style.display
来更改它是否可见。
let on = checkbox.checked;
document.querySelectorAll("p").forEach(e => {
e.style.display = on ? "block" : "none";
});
Note that you can use any selector inside document.querySelectorAll()
, including classes, attributes, IDs, etc.请注意,您可以在document.querySelectorAll()
中使用任何选择器,包括类、属性、ID 等。
Snippet:片段:
function change(){ var nav = document.getElementById("navbar"); var checkBox = document.getElementById("checkbox"); if(checkBox.checked == true){ nav.style.width = "300px"; } else{ nav.style.width = "70px"; } let on = checkbox.checked; document.querySelectorAll("p").forEach(e => { e.style.display = on? "block": "none"; }); }
nav{ display: flex; flex-direction: column; justify-content: space-between; width: 100px; height: 100%; transition: 0.3s; /* #63. change 2nd to -10px from -6px */ box-shadow: rgba(136, 165, 191, 0.26) 4px 2px 10px 0px, rgba(255, 255, 255, 0.8) -4px -2px 16px 0px; } nav a{ text-decoration: none; } nav li{ list-style-type: none; display: flex; color:black; } nav p{ display: none; } nav input{ margin: 10px; position: absolute; width: 50px; height: 50px; opacity: 0; cursor: pointer; } nav input:checked + nav p{ display: flex; }
<nav id="navbar"> <input onclick="change()" type="checkbox" name="" id="checkbox"> <ul> <a href="#"> <li> <p>Home</p> </li> </a> <a href="#"> <li> <p>Summer</p> </li> </a> </ul> </nav>
From JS change all 'p' color :从JS 更改所有 'p' 颜色:
Make all paragraphs display: none
and then with JS in your function change(): document.querySelectorAll('p').forEach(e => e.style.display = 'block');
使所有段落display: none
,然后在 function 中使用 JS change(): document.querySelectorAll('p').forEach(e => e.style.display = 'block');
function change(){ var nav = document.getElementById("navbar"); var checkBox = document.getElementById("checkbox"); if(checkBox.checked == true){ nav.style.width = "300px"; document.querySelectorAll('p').forEach(e => e.style.display = 'block'); } else{ nav.style.width = "70px"; document.querySelectorAll('p').forEach(e => e.style.display = 'none'); } }
nav{ display: flex; flex-direction: column; justify-content: space-between; width: 100px; height: 100%; transition: 0.3s; /* #63. change 2nd to -10px from -6px */ box-shadow: rgba(136, 165, 191, 0.26) 4px 2px 10px 0px, rgba(255, 255, 255, 0.8) -4px -2px 16px 0px; } nav a{ text-decoration: none; } nav li{ list-style-type: none; display: flex; color:black; } nav p{ display: none; } nav input{ margin: 10px; position: absolute; width: 50px; height: 50px; opacity: 0; cursor: pointer; } nav input:checked + nav p{ display: flex; }
<nav id="navbar"> <input onclick="change()" type="checkbox" name="" id="checkbox"> <ul> <a href="#"> <li> <p>Home</p> </li> </a> <a href="#"> <li> <p>Summer</p> </li> </a> </ul> </nav>
If you want to keep it as simple as possible, you can dynamically change the visibility property of the element.如果你想让它尽可能简单,你可以动态改变元素的可见性属性。 I would also personally not target the p>tags in this case but rather the whole list item, so that you don't render uneccessary DOM elements (ie there is no point in having an a tag and a list item if nothing will be displayed in it).在这种情况下,我个人也不会以 p>tags 为目标,而是以整个列表项为目标,这样您就不会呈现不必要的 DOM 元素(即,如果什么都不显示,则没有必要使用 a 标签和列表项在里面)。 So I would add an identifier to the a tag and toggle the visibility prop of the element:所以我会在 a 标签中添加一个标识符并切换元素的可见性属性:
Add a class to each list item:为每个列表项添加一个 class:
<nav id="navbar">
<input onclick="change()" type="checkbox" name="" id="checkbox">
<ul>
<a class="navbar-item" href="#">
<li>
<p>Home</p>
</li>
</a>
<a class="navbar-item" href="#">
<li>
<p>Summer</p>
</li>
</a>
</ul>
</nav>
Modify the onChange function to toggle the visibility:修改 onChange function 以切换可见性:
function change(){
var nav = document.getElementById("navbar");
var checkBox = document.getElementById("checkbox");
var navbarItems = document.getElementByClassname("navbar-item");
if(checkBox.checked == true){
nav.style.width = "300px";
navbarItems.style.visibility="visible";
} else {
nav.style.width = "70px";
navbarItems.style.visibility="hidden";
}
}
Remove this from the css:从 css 中删除:
nav p{
display: none;
}
Add this to the css:将此添加到 css:
.navbar-item {
visibility: 'hidden'
}
You should try to move away from inline
event handlers using onclick
and use an external event listener - keep the HTML as clean as possible.您应该尝试使用onclick
摆脱inline
事件处理程序并使用外部事件侦听器 - 尽可能保持 HTML 干净。 Use querySelector
to select specific nodes and querySelectorAll
to select a nodelist - which can be converted to an array if required quite easily using Array.from(nodelist)
- which then gives access to various array methods.使用querySelector
到 select 特定节点和querySelectorAll
到 select 一个节点列表 - 如果需要可以很容易地使用Array.from(nodelist)
将其转换为数组 - 然后可以访问各种数组方法。 In this case though that is not required - forEach
works on a nodelist so processing each p
element becomes simple.在这种情况下,虽然这不是必需的 - forEach
在节点列表上工作,因此处理每个p
元素变得简单。
document.querySelector('nav#navbar input[type="checkbox"]').addEventListener('click',function(e){ let col=this.parentNode.querySelectorAll('p'); col.forEach(p=>p.style.display=this.checked? 'block': 'none') this.parentNode.style.width=this.checked? '300px': '70px'; })
nav{ display: flex; flex-direction: column; justify-content: space-between; width: 100px; height: 100%; transition: 0.3s; /* #63. change 2nd to -10px from -6px */ box-shadow: rgba(136, 165, 191, 0.26) 4px 2px 10px 0px, rgba(255, 255, 255, 0.8) -4px -2px 16px 0px; } nav a{ text-decoration: none; } nav li{ list-style-type: none; display: flex; color:black; } nav p{ display: none; } nav input{ margin: 10px; position: absolute; width: 50px; height: 50px; opacity: 0; cursor: pointer; } nav input:checked + nav p{ display: flex; }
<nav id="navbar"> <input type="checkbox" value=1 /> <ul> <a href="#"> <li> <p>Home</p> </li> </a> <a href="#"> <li> <p>Summer</p> </li> </a> </ul> </nav>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.