无法在css中为下拉列表应用样式

时间:2018-03-19 04:20:37

标签: html css

您好我正在尝试使用css类设计多选下拉列表。我正在尝试开发下面的多选下拉列表框。

enter image description here

我有以下HTML代码。



.checkbox {
            position: relative;
            display: block;
            margin-top: 10px;
            margin-bottom: 10px;
        }
        .dropdown-menu {
            position: absolute;
            top: 100%;
            left: 0;
            z-index: 1000;
            float: left;
            min-width: 150px;
            max-height: 600px;
            overflow-x: visible;
            overflow-y: visible;
            padding: 0;
            margin: 0;
            list-style: none;
            font-size: 13px;
            font-weight: 500;
            text-align: left;
            background-color: #FFFFFF;
            border: 1px solid rgba(0, 0, 0, 0.15);
            border-radius: 0;
            -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
            -webkit-background-clip: padding-box;
            background-clip: padding-box;
            color: #464646;
        }

        .btn-group, .btn-group-vertical {
    position: relative;
    display: inline-block;
    vertical-align: middle;
}
toggle.btn-default {
    background: #dedede;
    background: rgba(0, 0, 0, 0.13);
    border: 1px solid #2E92FA;
    color: #464646;
    outline: none;
}

<div class="btn-group open">
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="true">Filter by<span class="caret"></span></button>
        <ul class="dropdown-menu" role="menu" style="width:100%;">
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <input type="checkbox" id="inlineCheckbox17" value="option1">
                    <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox17">Sensors</label>
                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <input type="checkbox" checked="checked" id="inlineCheckbox17" value="option1">
                    <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox17">Actuators</label>
                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <input type="checkbox" checked="checked" id="inlineCheckbox17" value="option1">
                    <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox17">Digital inputs</label>
                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <input type="checkbox" id="inlineCheckbox17" value="option1">
                    <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox17">Outputs</label>
                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <input type="checkbox" id="inlineCheckbox17" value="option1">
                    <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox17">Converters</label>
                </div>
            </li>
        </ul>
    </div>
&#13;
&#13;
&#13;

在上面的代码复选框中间,点击下拉菜单,它应该展开,默认情况下不应该展开。有人可以帮我按预期完成这项工作吗?任何帮助,将不胜感激。谢谢。

5 个答案:

答案 0 :(得分:2)

首先尝试删除open课程。

$('.btn-group').click(function(e) { $('.dropdown-menu').toggleClass('open'); });
body {
 margin:0;
}
.checkbox {
	position: relative;
	display: block;
	margin-top: 10px;
	margin-bottom: 10px;
}
.dropdown-menu {
	position: absolute;
	top: 100%;
	left: 0;
	z-index: 1000;
	float: left;
	min-width: 150px;
	max-height: 600px;
	overflow-x: visible;
	overflow-y: visible;
	padding: 0;
	margin: 0;
	list-style: none;
	font-size: 13px;
	font-weight: 500;
	text-align: left;
	background-color: #FFFFFF;
	border: 1px solid rgba(0, 0, 0, 0.15);
	border-radius: 0;
	-webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
	box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
	-webkit-background-clip: padding-box;
	background-clip: padding-box;
	color: #464646;
	transition:all .3s;
	transform: translate(-100%);
}
.dropdown-menu.open {
	transform: translate(0%);
}
.btn-group, .btn-group-vertical {
    position: relative;
    display: inline-block;
    vertical-align: middle;
}
toggle.btn-default {
    background: #dedede;
    background: rgba(0, 0, 0, 0.13);
    border: 1px solid #2E92FA;
    color: #464646;
    outline: none;
}
label.checkbox-inline {
    display: contents;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="true">Filter by<span class="caret"></span></button>
    <ul class="dropdown-menu" role="menu" style="width:100%;">
        <li>
            <div class="checkbox" style="margin-left:25px">
                <input type="checkbox" id="inlineCheckbox17" value="option1">
                <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox17">Sensors</label>
            </div>
        </li>
        <li>
            <div class="checkbox" style="margin-left:25px">
                <input type="checkbox" checked="checked" id="inlineCheckbox18" value="option1">
                <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox18">Actuators</label>
            </div>
        </li>
        <li>
            <div class="checkbox" style="margin-left:25px">
                <input type="checkbox" checked="checked" id="inlineCheckbox19" value="option1">
                <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox19">Digital inputs</label>
            </div>
        </li>
        <li>
            <div class="checkbox" style="margin-left:25px">
                <input type="checkbox" id="inlineCheckbox20" value="option1">
                <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox20">Outputs</label>
            </div>
        </li>
        <li>
            <div class="checkbox" style="margin-left:25px">
                <input type="checkbox" id="inlineCheckbox21" value="option1">
                <label style="margin-left:-30px; margin-right:30px;" class="checkbox-inline" for="inlineCheckbox21">Converters</label>
            </div>
        </li>
    </ul>
</div>

答案 1 :(得分:0)

<div class="btn-group open">中移除开放课程,并在点击过滤器按钮

时添加

答案 2 :(得分:0)

[demo] [1]在这里您可以在演示中找到代码

 [1]: http://jsfiddle.net/x2z08o3r/

答案 3 :(得分:0)

我相信你想要做的是添加一个javascript onclick函数,在点击过滤器时添加和删除你的复选框

let btn = document.querySelector('.btn-group');
let ul = document.querySelector('.dropdown-menu');

btn.addEventListener('click',function(){
    if(ul.style.display === 'none'){
        ul.style.display = 'block';  
  }else{ul.style.display = 'none'}
});

这是更新后的css

.checkbox {
            position: relative;
            display: block;
            margin-top: 10px;
            margin-bottom: 10px;
        }
        .dropdown-menu {
          display:none;
            position: absolute;
            top: 100%;
            left: 0;
            z-index: 1000;
            float: left;
            min-width: 150px;
            max-height: 600px;
            overflow-x: visible;
            overflow-y: visible;
            padding: 0;
            margin: 0;
            list-style: none;
            font-size: 13px;
            font-weight: 500;
            text-align: left;
            background-color: #FFFFFF;
            border: 1px solid rgba(0, 0, 0, 0.15);
            border-radius: 0;
            -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
            -webkit-background-clip: padding-box;
            background-clip: padding-box;
            color: #464646;
        }

        .btn-group, .btn-group-vertical {
    position: relative;
    display: inline-block;
    vertical-align: middle;
}
toggle.btn-default {
    background: #dedede;
    background: rgba(0, 0, 0, 0.13);
    border: 1px solid #2E92FA;
    color: #464646;
    outline: none;
}

答案 4 :(得分:0)

您可以查看以下内容

<强> CCS

.checkbox {
            position: relative;
            display: inline-block;
            margin-top: 10px;
            margin-bottom: 10px;
        }
        .dropdown-menu {
            position: relative;
            top: 100%;
            left: 0;
            z-index: 1000;
            float: left;
            min-width: 250px;
            max-height: 600px;
            overflow-x: visible;
            overflow-y: visible;
            padding: 0;
            margin: 0;
            list-style: none;
            font-size: 13px;
            font-weight: 500;
            text-align: left;
            background-color: #FFFFFF;
            border: 1px solid rgba(0, 0, 0, 0.15);
            border-radius: 0;
            -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
            -webkit-background-clip: padding-box;
            background-clip: padding-box;
            color: #464646;
        }

 .dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
}
.dropdown:hover .dropbtn {
    background-color: #3e8e41;
}

<强> HTML

<div class="btn-group open">
        <button type="button" class="dropbtn" data-toggle="dropdown" aria-expanded="true">Filter by<span class="caret"></span></button>
        <ul class="dropdown-menu" role="menu" style="width:100%;">
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <label style="margin-left:-15px; margin-right:15px;" class="checkbox-inline pull-right" for="inlineCheckbox17">Sensors</label>
                                        <input type="checkbox" id="inlineCheckbox17" value="option1">

                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <label style="margin-left:-15px; margin-right:15px;" class="checkbox-inline pull-right" for="inlineCheckbox17">Actuators</label>
                                        <input type="checkbox" checked="checked" id="inlineCheckbox17" value="option1">

                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <label style="margin-left:-15px; margin-right:15px;" class="checkbox-inline pull-right" for="inlineCheckbox17">Digital inputs</label>
                                        <input type="checkbox" checked="checked" id="inlineCheckbox17" value="option1">

                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <label style="margin-left:-15px; margin-right:15px;" class="checkbox-inline pull-right" for="inlineCheckbox17">Outputs</label>
                                        <input type="checkbox" id="inlineCheckbox17" value="option1">

                </div>
            </li>
            <li>
                <div class="checkbox" style="margin-left:25px">
                    <label style="margin-left:-15px; margin-right:15px;" class="checkbox-inline pull-right " for="inlineCheckbox17">Converters</label>
                                        <input type="checkbox" id="inlineCheckbox17" value="option1">

                </div>
            </li>
        </ul>
    </div>