2015-09-25 42 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