Dynamic dropdown menu with bootstrap not working

前端 未结 1 1464
粉色の甜心
粉色の甜心 2021-01-27 20:08

I have been using bootstrap for a while, and I came across an issue when trying to add a dropdown menu dynamically.

This is the JavaScript that I have:

$         


        
1条回答
  •  情书的邮戳
    2021-01-27 20:30

    Here is a working jsFiddle: http://jsfiddle.net/szx4Y/85/.

    FYI, your jsFiddle didn't work because you didn't include the Bootstrap script file. So if you were doing any testing there that wouldn't have worked.

    I changed your code to call $(".dropdown-toggle").dropdown(); immediately after you append your HTML. By calling that method you are setting up the elements to handle all the Bootstrap functionality (click events, etc); there's no reason to call it every time you click your new element.

    Relevant Code

    HTML

    
    
    
    

    Javascript

    $(document).ready(function() {
        $("#clickHere").click(function(){
                $("#appendHere").html("
    "); }); $('.dropdown-toggle').dropdown(); });

    CSS

    .dropdown-menu {
        float:right;
    }
    

    0 讨论(0)
提交回复
热议问题