2015-09-25 72 views
0

在引导程序站点的导航栏example上,我想以编程方式使其中一个Navbar项目(让我们说关于链接)禁用/不可点击。如何使Bootstrap导航栏项目不可点击?

我试过.addClass('disabled'),.removeClass('active'),但似乎没有任何工作。

我是否缺少一些简单的东西?

+0

此曲estion应该回答你的问题:http://stackoverflow.com/questions/13955667/disabled-href-tag。有关正确使用禁用的更多信息,请参阅此处:https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Attribute/disabled – Adjit

回答

1

如果你想在菜单项的点击没有做任何事情,你可以使用下面的jQuery的语法来禁用它的效果:

$(/* jQuery selector for menu item */).click(function(e) { 
    e.preventDefault(); 
}); 

然后你就可以用“禁用”或样式的按钮灰色,如果你愿意,以便用户知道它是不可点击的。该解决方案与支持jQuery的所有浏览器兼容。

下面是该解决方案的演示的jsfiddle(感谢@Adjit在评论!):http://jsfiddle.net/7a2p8jjn/1/

+1

要解决选项:http:// jsfiddle。 net/7a2p8jjn/1/ – Adjit

0

您只需添加pointer-events: none为元素的样式。 实施例与jquery:

$('.navbar-nav').find('a[href=#about]').css('pointer-events', 'none') 

P.S。:作为@马克西姆利-laumeister所述该溶液是not compatible with IE10 and earlier

+0

This works great :) –

+2

请小心,因为此解决方案[与IE10及更早版本不兼容](http://caniuse.com/#feat=pointer-events)。 –

0

只需添加类.disabled

<li class="disabled"><a href="#about">About</a></li> 

,然后在你的JavaScript

$(document).ready(function() { 
    $(".nav li.disabled").click(function() { return false; }); 
}); 

here you can see an example

当然也可以一类禁用添加到该项目与jQuery addClass method