Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k 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 Answer

0 votes
by (71.8m points)

try this make sure to put your dropdown id there

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...