Bootstrap 4导航栏下拉菜单在iOS Safari中无法正常工作求助
Hey there, I’ve dealt with this exact annoying problem before—iOS Safari really has some quirky behavior with Bootstrap’s navbar dropdowns. Let’s walk through the solutions that worked for me:
First, update your Bootstrap version
Older versions of Bootstrap (especially pre-4.6.x for Bootstrap 4, or early Bootstrap 5 builds) had known bugs with touch event handling on iOS. Swap in the latest stable release via your project’s package manager or CDN link—this alone fixes the issue for most cases.Stop event bubbling on submenu triggers
The root cause here is often that the click/touch event on your submenu link bubbles up to the parent hamburger menu’s toggle, causing the entire navbar to collapse. Add this JavaScript snippet to prevent that:For Bootstrap 4:
$('.dropdown-submenu a[data-toggle="dropdown"]').on('click touchstart', function(e) { e.stopPropagation(); // Prevent the event from reaching the parent navbar toggle if ($(this).next('.dropdown-menu').is(':visible')) { e.preventDefault(); // Only block default behavior when closing the submenu } $(this).next('.dropdown-menu').toggle(); });For Bootstrap 5 (uses
data-bs-toggleinstead):$('.dropdown-submenu a[data-bs-toggle="dropdown"]').on('click touchstart', function(e) { e.stopPropagation(); // Bootstrap 5 handles the toggle natively—we just need to block the event bubble });Ensure Safari recognizes clickable elements
Sometimes iOS Safari doesn’t register elements as clickable unless they have explicit cursor styling. Add this CSS to your submenu links:.dropdown-submenu a { cursor: pointer; }Check for custom JS conflicts
If you’ve added any custom JavaScript to handle navbar behavior, temporarily comment it out to see if that’s causing the conflict. Custom toggle logic often clashes with Bootstrap’s built-in event handlers on iOS.
Give these steps a try in order—starting with updating Bootstrap is the quickest win, and the event bubbling fix is the most reliable fallback if updating doesn’t do the trick.
内容的提问来源于stack exchange,提问作者feeva

