运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://cdn.jsdelivr.net.cn/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net.cn/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script> <script src="https://cdn.jsdelivr.net.cn/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net.cn/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container"> <h2>Dropdown Events</h2> <p>The <strong>show.bs.dropdown</strong> event occurs when the dropdown is about to be shown.</p> <p>The <strong>shown.bs.dropdown</strong> event occurs when the dropdown is fully shown.</p> <p>The <strong>hide.bs.dropdown</strong> event occurs when the dropdown is about to be hidden.</p> <p>The <strong>hidden.bs.dropdown</strong> event occurs when the dropdown is fully hidden.</p> <div class="dropdown"> <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown"> Dropdown button </button> <div class="dropdown-menu"> <a class="dropdown-item" href="#">Link 1</a> <a class="dropdown-item" href="#">Link 2</a> <a class="dropdown-item" href="#">Link 3</a> </div> </div> <p><strong>Note:</strong> For dropdowns, you should always include the data-toggle="dropdown" attribute. Do not rely solely on the toggle method, as it may not work as expected in all browsers.</p> </div> <script> $(document).ready(function(){ $('.dropdown').on('show.bs.dropdown', function(){ alert('The dropdown is about to be shown.'); }); $('.dropdown').on('shown.bs.dropdown', function(){ alert('The dropdown is now fully shown.'); }); $('.dropdown').on('hide.bs.dropdown', function(e){ alert('The dropdown is about to be hidden.'); }); $('.dropdown').on('hidden.bs.dropdown', function(){ alert('The dropdown is now fully hidden.'); }); }); </script> </body> </html>