简体   繁体   English

如何防止BootStrap Drop-down中URL末尾的#?

[英]How to prevent # at the end of URL in a BootStrap Drop-down?

I have this link: 我有这个链接:

<a href="#" id="myBtn"><i class="icon-user"></i><span>Add user</span></a>

I use preventDefault() and return false to avoid the # at the end of my address. 我使用preventDefault()return false以避免地址末尾的# And it works perfectly. 而且效果很好。 The problem is when I do the same with Bootstrap's dropdown. 问题是当我对Bootstrap的下拉列表做同样的事情时。 If I leave return false it doesn't add the # at the end as supposed, but it also prevents the dropdown from disappearing as it would if I remove return false . 如果我将return返回false,则不会按预期添加# ,但它也会阻止下拉消失,就像我删除return false This is my dropdown code: 这是我的下拉代码:

<li class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown" href="#" id="ddBtn">
    <i class="icon-wrench"></i><span>Parent Item<b class="caret"></b></span>
  </a>
  <ul class="dropdown-menu">
    <li><a href="#" id="ddSonBtn">Child item</a></li>

Javascript: 使用Javascript:

$("#ddSonBtn").click(function () {
    //some code

    e.preventDefault();
});

Is there a solution for this? 这有解决方案吗?

I am using 我在用

Bootstrap 2
jQuery

Thanks. 谢谢。

It may or may not be well documented, but there's no need to call both preventDefault() and return false; 它可能有也可能没有很好的文档记录,但是没有必要同时preventDefault() return false; in jQuery events. 在jQuery事件中。

EDIT : From jQuery's on docs: "Returning false from an event handler will automatically call event.stopPropagation() and event.preventDefault() " ( http://api.jquery.com/on/#event-handler ) 编辑 :从jQuery的on文档:“从事件处理程序返回false将自动调用event.stopPropagation()event.preventDefault() ”( http://api.jquery.com/on/#event-handler

return false; is the equivalent of e.preventDefault(); 相当于e.preventDefault(); and e.stopPropagation(); e.stopPropagation();

preventDefault prevents the default behavior of an element - such as a submit button submitting a form, an anchor navigating to a specific href , etc. preventDefault阻止元素的默认行为 - 例如提交表单的提交按钮,导航到特定href的锚点等。

stopPropagation stops the event from bubbling up the DOM. stopPropagation阻止事件冒泡DOM。 Meaning, events won't be triggered on parent elements of the target element. 意思是,不会在目标元素的父元素上触发事件。

Pick and choose which you need when working with Events. 选择使用事件时所需的选项。 In your situation, you probably only need to call preventDefault in your Dropdown's click handler. 在您的情况下,您可能只需要在Dropdown的单击处理程序中调用preventDefault

Also, in your code, the use of return in a function's scope is the last line of execution. 此外,在您的代码中,在函数范围内使用return是最后一行执行。 Any code after it will not be executed when the function is run. 运行该函数后, 不会执行其后的任何代码。

According to the Bootstrap docs: 根据Bootstrap文档:

http://getbootstrap.com/javascript/#dropdowns http://getbootstrap.com/javascript/#dropdowns

it is necessary to replace href by data-target attribute, ie 有必要用data-target属性替换href ,即

<a data-target="#" id="myBtn"><i class="icon-user"></i><span>Add user</span></a>

I believe it is more "bootstrapish" way to do things rather than suppressing event handlers or manually manipulating the show/hide behavior. 我认为这是做事的更“自助”的方式,而不是压制事件处理程序或手动操作显示/隐藏行为。

Just remove the # value from the href attribute. 只需从href属性中删除#值即可。 It's valid HTML to leave href empty. href留空是有效的HTML。

<a href="" id="myBtn"><i class="icon-user"></i><span>Add user</span></a>

用于Bootstrap + jQuery:

$(this).parent().parent().parent().removeClass("open")

I had the same issue and after much searching realised it was my own error. 我有同样的问题,经过多次搜索意识到这是我自己的错误。 I had an incorrect link to the bootstrap js file (the integrity hash was incorrect) and therefore didn't load. 我有一个错误的链接到bootstrap js文件(完整性哈希不正确),因此没有加载。 This caused the dropdown not to work on some pages. 这导致下拉列表无法在某些页面上运行。

I tried this way and it worked. 我尝试过这种方式并且有效。 The example (coffee script) is like this; 示例(咖啡脚本)是这样的;

jQuery ->
  $('.your_mark ul.dropdown-menu li').click ->
    # Do what you want
    $(this.parentElement.parentElement).removeClass('open')
    return false
  1. Close the dropdown menu manually by removing css class 'open' from the btn-group div. 通过从btn-group div中删除css类'open'来手动关闭下拉菜单。
  2. Return false to avoid having # in your url. 返回false以避免在您的网址中包含#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM