html - 根据 slider (复选框)选择显示/隐藏 div

标签 html css

有没有办法根据 slider 的位置来显示/隐藏 hiddencar div?我不确定如何使用 :target 选择器,我相信它可以单独使用 css 来完成

是显示div 没有隐藏div

	    .onoffswitch4 {
        position: relative; width: 101px;
        -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
    }
    .onoffswitch-checkbox4   {
        display: none;
    }
    .onoffswitch-label4 {
        display: block; overflow: hidden; cursor: pointer;
        border: 2px solid ; border-radius: 11px;
    }
    .onoffswitch-inner4 {
        display: block; width: 200%; margin-left: -100%;
        transition: margin 0.3s ease-in 0s;
    }
    .onoffswitch-inner4:before, .onoffswitch-inner4:after {
        display: block; float: left; width: 50%; height: 38px; padding: 0; line-height: 38px;
        font-size: 13px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
        box-sizing: border-box;
    }
    .onoffswitch-inner4:before {
        content: "No";
        padding-left: 14px;
        background-color: #34A7C1; color: #FFFFFF;
    }
    .onoffswitch-inner4:after {
        content: "Yes";
        padding-right: 14px;
        background-color: #EEEEEE; color: #999999;
        text-align: right;
    }
    .onoffswitch-switch4 {
        display: block; width: 9px; margin: 14.5px;
        background: #FFFFFF;
        position: absolute; top: 0; bottom: 0;
        right: 59px;
        border: 2px solid ; border-radius: 11px;
        transition: all 0.3s ease-in 0s; 
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-inner4 {
        margin-left: 0;
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-switch4 {
        right: 0px; 
    }
   <div id="contact_phone">Do you have a car?: *<br>
     <div class="onoffswitch4">
        <input type="checkbox" name="onoffswitch4" class="onoffswitch-checkbox4" id="myonoffswitch4" checked>
        <label class="onoffswitch-label4" for="myonoffswitch4">
            <span class="onoffswitch-inner4"></span>
            <span class="onoffswitch-switch4"></span>
        </label>
    </div>
    
    
<div id="hiddencar">							
				
	<div id="contact_name">Year: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div> 
	
        
	<div id="contact_phone">Make: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
	</div>    
      
      
     <div id="contact_phone">Model: *<br>
     <input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div>   
    	
</div><!--hiddencar-->
    
    
    
   </div><!--contact_phone-->

有没有办法根据 slider 的位置来显示/隐藏 hiddencar div?我不确定如何使用 :target 选择器,我相信它可以单独使用 css 来完成

是显示div 没有隐藏div

最佳答案

出于某种原因,CSS slider 实际上并没有改变复选框的选中属性,我在我的 JS 解决方案中添加了这个以及一个 showHideCarFields 函数,该函数将根据需要显示或隐藏汽车字段

$(document).ready(function() {
  $(document).on("click", ".onoffswitch4", function() {
    $("#myonoffswitch4").prop("checked", !$("#myonoffswitch4").prop("checked"))
    showHideCarFields();
  });
  showHideCarFields();
});

function showHideCarFields() {
    var isChecked = $('#myonoffswitch4').prop("checked");
    if(isChecked) {
      $('#hiddencar').hide();
    } else {
      $('#hiddencar').show();
    }
}
.onoffswitch4 {
        position: relative; width: 101px;
        -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
    }
    .onoffswitch-checkbox4   {
        display: none;
    }
    .onoffswitch-label4 {
        display: block; overflow: hidden; cursor: pointer;
        border: 2px solid ; border-radius: 11px;
    }
    .onoffswitch-inner4 {
        display: block; width: 200%; margin-left: -100%;
        transition: margin 0.3s ease-in 0s;
    }
    .onoffswitch-inner4:before, .onoffswitch-inner4:after {
        display: block; float: left; width: 50%; height: 38px; padding: 0; line-height: 38px;
        font-size: 13px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
        box-sizing: border-box;
    }
    .onoffswitch-inner4:before {
        content: "No";
        padding-left: 14px;
        background-color: #34A7C1; color: #FFFFFF;
    }
    .onoffswitch-inner4:after {
        content: "Yes";
        padding-right: 14px;
        background-color: #EEEEEE; color: #999999;
        text-align: right;
    }
    .onoffswitch-switch4 {
        display: block; width: 9px; margin: 14.5px;
        background: #FFFFFF;
        position: absolute; top: 0; bottom: 0;
        right: 59px;
        border: 2px solid ; border-radius: 11px;
        transition: all 0.3s ease-in 0s; 
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-inner4 {
        margin-left: 0;
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-switch4 {
        right: 0px; 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contact_phone">Do you have a car?: *<br>
     <div class="onoffswitch4">
        <input type="checkbox" name="onoffswitch4" class="onoffswitch-checkbox4" id="myonoffswitch4" checked>
        <label class="onoffswitch-label4" for="myonoffswitch4">
            <span class="onoffswitch-inner4"></span>
            <span class="onoffswitch-switch4"></span>
        </label>
    </div>
    
    
<div id="hiddencar">							
				
	<div id="contact_name">Year: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div> 
	
        
	<div id="contact_phone">Make: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
	</div>    
      
      
     <div id="contact_phone">Model: *<br>
     <input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div>   
    	
</div><!--hiddencar-->
    
    
    
   </div><!--contact_phone-->

关于html - 根据 slider (复选框)选择显示/隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50801401/

相关文章:

javascript - 如何将 div 高度设置为用户显示器分辨率的 100%?

html - 在 SPAN 元素上设置禁用的属性不会阻止单击事件

javascript - 动态应用 CSS 过滤器

javascript - scrollTo延迟页面过渡效果

javascript - 如何缩短长重复路径和组合语句

javascript - 未捕获的类型错误 : Cannot read property "top" of null

javascript - 在外面点击如何恢复正常? [Javascript]

html - 如何在将 enml 转换为 html 时将 en-media 转换为 img

html - 如何使用边距设置标签栏之间的空格?

javascript - Windows Phone 8 IE 1px 水平滚动条