Bootstrap 5一级菜单改成可以点击

发布时间:2025-06-13点击次数:6

if ($(window).width() >= 992) {

      $('.navbar .dropdown > .nav-link').on('click', function(e) {

        // 如果点击的是有下拉菜单的链接

        if ($(this).next('.dropdown-menu').length) {

          // 阻止默认的Bootstrap事件

          e.preventDefault();

          e.stopPropagation();


          // 如果当前是展开状态则跳转,否则先展开

          if ($(this).parent().hasClass('show')) {

            window.location.href = $(this).attr('href');

          } else {

            $(this).parent().addClass('show');

            $(this).next('.dropdown-menu').addClass('show');

          }

        }

      });


      // 鼠标悬停展开下拉菜单

      $('.navbar .dropdown').hover(function() {

        $(this).addClass('show');

        $(this).find('.dropdown-menu').addClass('show');

      }, function() {

        $(this).removeClass('show');

        $(this).find('.dropdown-menu').removeClass('show');

      });

    }