简体   繁体   English

选中复选框输入时,如何在另一个元素中选择div?

[英]How to select a div inside another element when a checkbox input is checked?

I have a checkbox that I need when it's checked to show a div inside another element (not next to the checkbox it's inside a div that is next to it's parent) 我有一个复选框,当它被检查以显示另一个元素内的div时(不在复选框旁边,它位于它的父元素旁边的div中)

 <nav>
   <div class='container'>
     <label class="tog" for="toggle">&#9776;</label>
     <input type="checkbox" id="toggle"></input> //This is the checkbox
   </div>
 </nav>
 <div class='container'>
   <div class='select'> // This is the div that I want to select when checkbox is checked
   </div>
 </div>

I tried these selectors but didn't work: 我尝试了这些选择器,但没有工作:

#toggle:checked   ~ .select{display:block}
#toggle:checked   + .select{display:block}

There is not parent selector in CSS. CSS中没有父选择器。 If you can change the HTML, move the input to the same level of nav and the div's .container . 如果您可以更改HTML,请将输入移动到相同级别的nav和div的.container Hide the input, which is controlled by the label, and using CSS sibling and parent rules you can control the div display property: 隐藏由标签控制的输入,并使用CSS兄弟和父规则控制div display属性:

 #toggle { height: 0; overflow: hidden; padding: 0; margin: 0; } #toggle:checked + nav .tog { color: red; } #toggle:not(:checked) ~ .container > .select { display: none; } 
 <input type="checkbox" id="toggle"> <nav> <div class="container"> <label class="tog" for="toggle">&#9776;</label> </div> </nav> <div class="container"> <div class="select"> // This is the div that I want to select when checkbox is checked </div> </div> 

This will solve your problem with JS easily. 这将很容易解决您的JS问题。

var input = document.getElementById('toggle');
var select = document.querySelectorAll('.select')[0];
input.onchange = function(){
   select.style.display = this.checked? 'block': 'none';
}

I haven't tried this, but if you need to show/hide a div based on the status of a checkbox, you can try this 我没试过这个,但是如果你需要根据复选框的状态显示/隐藏div,你可以尝试这个

    <nav>
        <div class='container'>
            <label class="tog" for="toggle">&#9776;</label>
            <input type="checkbox" id="toggle"></input> //This is the checkbox
        </div>
    </nav>

<div class='container'>
    <div class='select' id="div-to-show" style="display:none">
    </div>
</div>

var check = document.getElementById('toggle');
check.addEventListener( 'click', function( event ){ 
    if( check.checked === true ){ 
        document.getElementById('div-to-show').style.display = 'block'; 
    }
    else{
        document.getElementById('div-to-show').style.display = 'none';
    }
} );

The key part of your problem is here 问题的关键部分在这里

it's inside a div that is next to it's parent 它在一个div旁边,就在它的父母旁边

This can't be done with CSS right now, as the target needs to be within the same parent. 目前无法通过CSS完成此操作,因为目标需要位于同一父级中。 You can look easwee's answer to this question to get a little more in-depth explanation, or BoltClock's answer to get MUCH more in-depth . 您可以看一下这个问题的答案,以获得更深入的解释,或者BoltClock的答案可以获得更深入的内容

Obviously, this can be done with Javascript if you want to go down that route, but it sounded like you wanted a CSS answer. 显然,如果你想沿着这条路走下去,这可以用Javascript来完成,但听起来你想要一个CSS答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM