Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

javascript - Prevent Dropdown menu from closing if I click a menu item

I have a drop down menu in sidebar and it has links as menu items. When I click a menu item(link) then link works fine but drop down menu gets closed.

But I want that dropdown menu stay opened even after clicking a menu item.

HTML Code:

<div id="sidebar-wrapper">
     <div class="sidebar-heading">Start Bootstrap </div>
     <div class="list-group list-group-flush">              
          <a href="{% url 'shop:admin_orders_list' %}">All orders</a>

          <button class="dropdown-btn button">Products
               <i class="fa fa-caret-down"></i>
          </button>

          <div class="dropdown-container">
               <a href="{% url 'shop:admin_products_list' %}">View Products</a>
               <a href="{% url 'shop:admin_product_create' %}">Add New Product</a>
          </div>   
     </div>
</div>

I tried these two following ways:

$('div.dropdown-container a').on('click', function(e) {
        e.preventDefault();
});
$('div.dropdown-container a').on('click', function(e) {
        e.stopPropagation();
});

But these two ways did not work.

Please help me to resolve it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this make sure to put your dropdown id there

$('#dropdownid').on('hide.bs.dropdown', function () {
    return false;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...