twitter-bootstrap 如何禁用 JQuery 选项卡上的单击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20516174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 19:27:16  来源:igfitidea点击:

How to disable click event on JQuery tabs?

jqueryhtmltwitter-bootstraptabs

提问by ciaoben

I have these simple tabs:

我有这些简单的标签:

<div class="ftabs">                    

<ul id="tabs" class="tabs nav nav-tabs nav-justified" data-tabs="tabs">
    <li  id='an'><a href="#anag" data-toggle="tab"><h3>Dati Anagrafici</h3></a></li>
    <li id='ind'><a href="#indirizzi"  data-toggle="tab"><h3>Indirizzi</h3></a></li>
    <li id='ref'><a href="#referenti" data-toggle="tab"><h3>Referenti</h3></a></li>
    <li id='ban'><a href="#blue" data-toggle="tab"><h3>Banche</h3></a></li>
</ul>

I can open the tab that I want with jQuery, for example:

我可以用 jQuery 打开我想要的选项卡,例如:

$("#ref a[href='#referenti']").tab('show');

but I would like to disable the first 2 tabs. I've tried it this way, but it doesn't work:

但我想禁用前 2 个选项卡。我已经尝试过这种方式,但它不起作用:

$('#tabs').tabs('option', 'disabled', [0, 1]);

Why? How can I do this?

为什么?我怎样才能做到这一点?

回答by Arun P Johny

You cannot disable the active tab, so need to set the 3rd tab as active then disable the first two

您无法禁用活动选项卡,因此需要将第三个选项卡设置为活动选项卡,然后禁用前两个选项卡

From Docs:

从文档

The selected tab cannot be disabled.

So

所以

jQuery(function ($) {
    $("#tabs").tabs({
        active: 2,
        disabled: [0, 1]
    });
});

Demo: Fiddle

演示:小提琴

To disable the tabs programatically later

稍后以编程方式禁用选项卡

$("#tabs").tabs("option", "active", 2)
$("#tabs").tabs("option", "disabled", [0, 1]);

Demo: Fiddle

演示:小提琴

回答by EoiFirst

I would use the jquery index()(http://api.jquery.com/index/) function. With it you can find out the index of the element you're passing as argument doing like this :

我会使用 jquery index()( http://api.jquery.com/index/) 函数。有了它,您可以找出作为参数传递的元素的索引,如下所示:

//selector could be wrong but you have the use logic
$(".tabs > li").click(function() {
    if ($(".tabs > li").index($(this)) > 1) {
        $(this).tab('show');
    }
});