简体   繁体   English

我如何 select 所有 p 标签并在单击复选框时使它们可见?

[英]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>

In your case you need to add to CSS and it should work.在您的情况下,您需要添加到 CSS,它应该可以工作。

nav input:checked + ul p {
  display: block;
}

Result you can see in jsfiddle您可以在jsfiddle中看到的结果

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.

相关问题 单击按钮时如何创建多个标签? 当它们太多时,如何使它们消失? - How do I create multiple tags when a button is clicked? How do I make them disappear when there is too many of them? 当我使用jQuery单击选择按钮时,如何选择所有复选框 - How do I select all check box when I click on Select button using jQuery 单击时如何更改复选框的“选中”属性? - How do I change the “checked” attribute of a check box when clicked? 在我手动选中和取消选中复选框后,如何使复选框响应 select all function? - How do I make check-boxes respond to a select all function after I manually checked and unchecked them? 如何使我的全选复选框忽略禁用的项目? - How can I make my select all check box ignore disabled items? 如何使用jQuery忽略发送到服务器的元素列表中的“全选”复选框? - How do I use jQuery to omit the “Select All” check box from the list of elements sent to the server? 多次单击时如何使一个按钮选中“取消选中”复选框? - How to make One button check Uncheck check box when clicked more than once? 如何删除所有 <br /> 不在标签内 <p> 标签使用正则表达式? - How do I remove all <br /> tags that aren't within <p> tags using regex? 单击按钮时如何缩短所有div框? - How do I make all div boxes shorten when a button is clicked? 我如何包装所有文本节点 <p> 在div中可能还包含其他 <p> 标签和<strong>使用jQuery之类的</strong>标签<strong>?</strong> - How do I wrap all text nodes with <p> in a div that also may contain other <p> tags and tags such as <strong> using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM