twitter-bootstrap 在引导程序的下拉列表中动态创建项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24859697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 21:09:37  来源:igfitidea点击:

dynamically create items in drop down list in bootstrap

javascriptjquerytwitter-bootstrap

提问by user3840570

Small part of my html code :

我的 html 代码的一小部分:

<div class="text-center">

        <div class="btn-group">
            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                Platforms <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" role="menu" id = "buttonsPlace">
            </ul>

        </div>

In my js file :

在我的 js 文件中:

for (i = 0; i < platformList.length; i++) {
        var li =  $("<li/>" , { id : "plat"+i,class : "dropdown" text : platformList[i]  } )
        //var text = document.createTextNode(platformList[i]);
        //li.appendChild(text);
        //btn.data("platform", platformList[i] );
         $("#buttonsPlace").append(li);
        console.log("hiding");
        $("#plat" + i).hide();
    }

However the menu is appearing but the menu items are not. where am i going wrong

但是菜单出现但菜单项没有。我哪里错了

回答by cracker

Try This

试试这个

$(function() {
    var change = function( txt ) {
        $("#ID").append( '<li>' + txt + '</li>' );
    };
    change("this is the first change");
    change("this is the second change");
});

Demo

演示

For Li Click

对于李点击

$("ul").on('click', 'li', function () {
   var id = this.id;
   //play with the id
});

回答by Alex Chizhov

$(function(){

    $('.dropdown-toggle').click(function(){

        var countRows = $('ul.dropdown-menu li').size();
        $('.dropdown-menu').append('<li>Row '+countRows+'</li>');
        countRows++;
    });

});

Here is the jsfiddle for you http://jsfiddle.net/alexchizhov/ncgXK/

这是你的 jsfiddle http://jsfiddle.net/alexchizhov/ncgXK/

$('#drowdown-link1').click(function(e){
   e.preventDefault();
   window.location.href = 'http://example.com';
});

Here is another jsfiddle for you http://jsfiddle.net/alexchizhov/ncgXK/4/

这是你的另一个 jsfiddle http://jsfiddle.net/alexchizhov/ncgXK/4/