【问题标题】:Custom dropdown list not working自定义下拉列表不起作用
【发布时间】:2014-07-17 06:02:25
【问题描述】:

我正在尝试创建一个自定义下拉列表。我希望在我的下拉列表中有两个选项,即预定义和自定义。当我从列表中选择自定义时,它没有被选中。到目前为止,我有这段代码不起作用。

    <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>

【问题讨论】:

  • 为什么会起作用...点击自定义 - 或任何下拉菜单中的任何内容都没有附加事件?
  • 我是 javascript/jquery 的新手。您能否修复我的代码以使其正常工作。

标签: javascript jquery html css drop-down-menu


【解决方案1】:

您必须将事件侦听器附加到导航项才能使其工作,否则您编写了所有这些 javascript 并且没有调用它来工作。

Event Listeners

【讨论】:

  • +1,但在堆栈溢出时,我们鼓励用户在答案中包含链接内容的摘要。这样,如果链接失效,答案仍然有效。
【解决方案2】:

这就是你想要的吗?我只是使用 jquery 来交换点击事件上的文本,我认为需要对你的 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");
}
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2019-02-03
    相关资源
    最近更新 更多