我正在尝试创建一个自定义下拉列表。我想在我的下拉列表中有两个选项,即预定义和自定义。当我从列表中选择自定义时,它没有被选中。到目前为止我有这个代码无效。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function DropDown(el) {
this.dd = el;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;
obj.dd.on('click', function(event){
$(this).toggleClass('active');
event.stopPropagation();
});
}
}
$(function() {
var dd = new DropDown( $('#dd') );
$(document).click(function() {
// all dropdowns
$('.wrapper-dropdown-5').removeClass('active');
});
});
</script>
<style>
.wrapper-dropdown-5 {
/* Size & position */
position: relative;
width: 144px;
margin: 0 auto;
padding: 12px 15px;
/* Styles */
background: #fff;
cursor: pointer;
outline: none;
transition: all 0.3s ease-out;
}
.wrapper-dropdown-5:after { /* Little arrow */
content: "";
width: 0;
height: 0;
position: absolute;
top: 50%;
left: 165px;
margin-top: -3px;
border-width: 6px 6px 0 6px;
border-style: solid;
border-color: #067ab4 transparent;
}
.wrapper-dropdown-5 .dropdown {
/* Size & position */
position: absolute;
top: 30%;
left: -40;
right: 0;
/* Styles */
list-style: none;
transition: all 0.3s ease-out;
/* Hiding */
max-height: 0;
overflow: hidden;
}
.wrapper-dropdown-5 .dropdown li {
padding: 0 10px ;
}
.wrapper-dropdown-5 .dropdown li a {
display: block;
text-decoration: none;
color: #067ab4;
transition: all 0.3s ease-out;
}
.wrapper-dropdown-5 .dropdown li:last-of-type a {
border: none;
}
.wrapper-dropdown-5 .dropdown li i {
margin-right: 5px;
color: inherit;
vertical-align: middle;
}
/* Hover state */
.wrapper-dropdown-5 .dropdown li:hover a {
color: #57a9d9;
}
/* Active state */
.wrapper-dropdown-5.active {
border-radius: 5px 5px 0 0;
box-shadow: none;
border-bottom: none;
color: white;
}
.wrapper-dropdown-5.active:after {
border-color: #067ab4 transparent;
}
.wrapper-dropdown-5.active .dropdown {
border-bottom: 0px solid rgba(0,0,0,0.2);
max-height: 400px;
}
div#dd
{
color: #067ab4;
font: 30px tahoma;
display: inline-block;
}
div#textA
{
display: inline-block;
font: 30px tahoma;
padding-left: 20px;
}
div#textB
{
display: inline-block;
font: 30px tahoma;
padding-left: 6px;
}
</style>
</head>
<body> <div id="textA">I want to select a</div>
<div id="dd" class="wrapper-dropdown-5" tabindex="1">Predefined
<ul class="dropdown">
<li><a href="#"><i class="icon-user"></i>Custom</a></li>
</ul>
</div>
<div id="textB">profile</div>
</body>
<html>
答案 0 :(得分:2)
您必须将一个事件监听器附加到导航项才能使其正常工作,否则您将编写所有这些javascript,并且无需调用它。
答案 1 :(得分:1)
这是你要去的吗?我只是使用jquery在click事件上交换文本,我需要对你的css进行一些调整,但要让它换回来。
这里是小提琴:http://jsfiddle.net/R5aHa/
$('#dd2').click(function(){
var selected = $('#dd2').text();
if($('#dd2').text(selected)){
$('#dd2').html("Predefined");
$('#dd').html("Custom");
} else {
$('#dd').html("Predefined");
$('#dd2').html("Custom");
}
});