【问题标题】:Bootstrap dropdown menu on mouseenter with active links带有活动链接的 mouseenter 上的引导下拉菜单
【发布时间】:2021-10-07 13:23:25
【问题描述】:

我正在尝试在 mouseenter 上显示引导下拉菜单。这没问题,只需要一些jquery,比如:

$('.dropdown-toggle').on('mouseenter', function () {
   $('.dropdown-toggle').dropdown('toggle')
});

我需要的是例如 div 分为两个部分。在每个部分中都会有指向某些内容的链接(使用 Nette 框架,因此会有指向特定操作和演示者的链接)。 但我无法实现。如果下拉菜单有效,则链接无效。

结构应该是这样的:

<div class="dropdown-toggle" data-toggle="dropdown">
  <div>
    <a href="{plink :User:Dashboard:default, 'userId' => $user->getId()}">
      <span class="text-white"><i class="fa fa-user"></i> {$userData["user"]["name"]}</span>
    </a>
  </div>
  <div>
    <a href="{plink :Account:Edit:default}">
      <i class="fa fa-plus-square text-white btn-xs"></i><span class="text-warning"> {_}Není nastaven aktivní účet{/_}</span>
    </a>
  </div>
</div>

找到了一些技巧,使用 data-target 属性,但不知道具体如何。 https://getbootstrap.com/

编辑: jQuery:

  $('.dropdown-toggle').on('mouseenter', function () {
    $(this).dropdown('toggle');
    $('.dropdown-toggle>div>a').on('click', function () {
      location.href = $(this).attr('href');
    });
  });

  $('.dropdown').on('mouseleave', function () {
    $('.dropdown-toggle').dropdown('toggle');
  });

结构:

<button class="dropdown-toggle" data-toggle="dropdown">
  <div>
    <a href="{plink :User:Dashboard:default, 'userId' => $user->getId()}">
      <span class="text-white"><i class="fa fa-user"></i> {$userData["user"]["name"]}</span>
    </a>
  </div>
  <div>
    <a href="{plink :Account:Edit:default}">
      <i class="fa fa-plus-square text-white btn-xs"></i><span class="text-warning"> {_}Není nastaven aktivní účet{/_}</span>
    </a>
  </div>
</button>

【问题讨论】:

  • 所以我以某种方式做到了。我将两个 div 包裹在 &lt;button class="btn btn-link dropdown-toggle" data-toggle="dropdown"&gt;&lt;/button&gt; 中。添加了“btn”和“btn-link”类,所以它看起来很正常,而不是像按钮。并像这样更新 jquery 代码$('.dropdown-toggle').on('mouseenter', function () { $(this).dropdown('toggle'); $('.dropdown-toggle&gt;div&gt;a').on('click', function () { location.href = $(this).attr('href'); }); }); $('.dropdown').on('mouseleave', function () { $('.dropdown-toggle').dropdown('toggle'); });
  • 好的,所以你确实采用了丑陋的解决方案。您使用的是 location.href,而不是 window.open。大声笑

标签: html jquery dropdown nette


【解决方案1】:

mouseenter 上,您可以将下拉菜单的css 更改为显示,在mouseleve 上,再次隐藏它。试试这样的:

$('.dropdown-toggle').on('mouseenter', function () {
   $('.dropdown-toggle').dropdown('toggle');
   $('.dropdown-menu').css('display', 'flex');
});

$('.dropdown-menu').on('mouseleave', function () {
   $('.dropdown-menu').css('display', 'none');
});
.dropdown-menu div{
  color: #fff;
  padding: 1rem;
}

.dropdown-menu div:first-child{
  background: blue;
}

.dropdown-menu div:last-child{
  background: red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="dropdown">
  <button id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="dropdown-toggle">
    Dropdown trigger
    <span class="caret"></span>
  </button>
  <div class="dropdown-menu" aria-labelledby="dLabel">
    <div>
      <h1>
        Left Column
      </h1>
    </div>
    <div>
      <h1>
        Right Column
      </h1>

    </div>
  </div>
</div>

【讨论】:

  • 嗯,这只是鼠标悬停的工作解决方案。但不适用于里面的链接。
  • 您是否查看了开发者工具中的链接?如果 href 不起作用,那么您可以在 &lt;a&gt; 标签上使用点击事件,然后使用 window.open 打开链接
  • 这行得通,但它是一种丑陋的解决方案。必须有简单的解决方案。
  • 好吧,我看到尽管它是一个丑陋的解决方案,但您仍然决定采用它。如果您期望一个准确的解决方案,那么至少提供一个可重现的示例或您的代码在文档加载后的外观快照。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 2015-09-30
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 2016-01-27
  • 2013-09-22
相关资源
最近更新 更多