【问题标题】:Don't want to trigger modal when clicking inside dropdown inside same parent element在同一父元素内单击下拉菜单时不想触发模式
【发布时间】:2020-08-21 07:37:49
【问题描述】:

在下面运行这段 Bootstrap HTML 和 JS 代码,我正在努力在单击下拉导航链接(例如标记为已读)时启动模式。

 <ul id="notification_items" class="navi navi-hover">
  <!--begin::Item-->
  <li id="1f7cbbe4-2345-486a-ab66-b05601f033a9" data-notification-id="1f7cbbe4-2345-486a-ab66-b05601f033a9" data-notification-subject="Some Subject" data-notification-message="And some message" class="notification_item cursor-pointer navi-item">
    <div class="pr-10 pl-2 navi-link">
      <div class="navi-label mx-5"><span class="label label-dot label-lg label-primary mt-n6"></span></div>
      <div class="navi-text">
        <span class="font-weight-bolder font-size-lg">Some Subject</span><span class="text-muted"> - And some message</span>
        <div class="text-muted font-size-sm">3 hours ago</div>
      </div>
      <div class="notification_actions dropdown dropdown-inline d-none">
        <a href="javascript:;" class="btn btn-sm btn-clean btn-icon btn-hover-transparent-primary mr-2" data-toggle="dropdown" aria-expanded="false"><i class="far fa-ellipsis-h"></i></a>
        <div class="dropdown-menu dropdown-menu-sm dropdown-menu-right" style="">
          <ul class="navi flex-column navi-hover py-2">
            <li class="navi-item"><span data-notification-id="1f7cbbe4-2345-486a-ab66-b05601f033a9" class="notification_mark_as_read navi-link"><span class="navi-icon"><i class="fad fa-comment-check"></i></span><span class="navi-text">Mark as Read</span></span></li>
            <div class="dropdown-divider"></div>
            <li class="navi-item"><a href="#" class="navi-link" data-toggle="modal"><span class="navi-icon"><i class="far fa-trash-alt text-danger"></i></span><span class="navi-text text-danger">Delete</span></a></li>
          </ul>
        </div>
      </div>
    </div>
  </li>
  <!--end::Item-->
</ul>

以及点击&lt;li&gt;时显示模态的简单JS代码:

$(document).on("click", ".notification_item", function () {
   $('#notification').modal('show');
});

我在点击.notification_actions 时尝试了e.stopPropagation();,它最初有效,但显然其他所有事件都不再有效。

【问题讨论】:

标签: javascript jquery twitter-bootstrap bootstrap-modal


【解决方案1】:

您必须使用点击事件的事件参数中的参数:targetcurrentTarget

  • target 将返回发生点击的 DOM 元素 最初

  • currentTarget 将始终是处理程序所在的 DOM 元素 附上 -> 在你的情况下li.notification_item

解决方案是识别发生点击的元素 - target。有很多方法 - 在您的情况下,您可以通过遍历 DOM 直到根菜单 (#notification_items) 来检测您的点击是否发生在下拉导航链接 (.dropdown-menu) 中:

$(document).on("click", ".notification_item", function (e) {
// you traverse up to the root ul, and looking if you are in a dropdown menu
    if ($(e.target).closest('.dropdown-menu', '#notification_items').length > 0) {
    // You are in a drop down menu -> do nothing
  } else {
    // you are somewhere else -> trigger the modal
    $('#notification').modal('show');
  }
});

JSFiddle

附:此代码检查您是否在下拉菜单中,如果您想检查特定下拉菜单,可以使用特定选择器。

【讨论】:

  • 感谢您花时间解释我需要做的事情,感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 2015-12-04
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多