jQuery 禁用引导选项卡点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20019384/
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-08-27 00:54:50  来源:igfitidea点击:

Disable bootstrap tab clicks

jquerytwitter-bootstrap

提问by user2999253

I am using bootsrap tabs for a registration form , I changed navigation of tabs using onclick event of next and previous button . But still that tabs click works and being able to go the desired page easily Please help how can i stop tab click navigation i only want next previous button navigation Please find the html code below

我正在使用引导程序选项卡作为注册表单,我使用下一个和上一个按钮的 onclick 事件更改了选项卡的导航。但是标签点击仍然有效并且能够轻松进入所需页面请帮助我如何停止标签点击导航我只想要下一个上一个按钮导航请找到下面的html代码

 <ul class="nav nav-tabs" id="myTab">
   <li class="active">
     <a href="#home" data-toggle="tab">1.PERSONAL DETAILS</a> 
   </li>   
   <li>
     <a href="#profile" data-toggle="tab"> 2. CONTACT DETAILS</a> 
   </li>  
   <li>
     <a href="#messages" data-toggle="tab">3. EDUCATION DETAILS</a>
   </li>   
   <li>
     <a href="#course" data-toggle="tab">4. SELECT COURSE</a>
   </li>   
   <li>
     <a href="#settings" data-toggle="tab">5. PAYMENT DETAILS</a>
   </li> 
 </ul>
 <div class="tab-content">
    <div class="tab-pane active" id="home">Form elements </div>
    <div id="profile">Form elements </div>
    <div id="messages">Form elements </div>
    <div id="settings">Form elements </div>
 </div>

回答by JenonD

I have tabs like this in the HTML (using Bootstrap 3.0):

我在 HTML 中有这样的选项卡(使用 Bootstrap 3.0):

<ul class="nav nav-tabs" id="createNotTab">
    <li class="active" ><a href="#home" data-toggle="tab">Step 1: Select Property</a></li>
    <li class="disabled"><a href="#createnotification" data-toggle="" >Step 2: Create Notification</a></li>
</ul>

Next/Previous buttons

下一个/上一个按钮

<button class="btn btn-warning prevtab" type="button" onclick="return showPrev()"><span class="glyphicon glyphicon-arrow-left"></span>Previous </button>
 <button class="btn btn-info prevtab" type="button" onclick="return showNext()"><span class="glyphicon glyphicon-arrow-right"></span>Next </button>

In my JavaScript file:

在我的 JavaScript 文件中:

 var $tabs = $('#createNotTab li');


function showPrev() {
    $tabs.filter('.active').prev('li').removeClass("disabled");
    $tabs.filter('.active').prev('li').find('a[data-toggle]').each(function () {
       $(this).attr("data-toggle", "tab");
    });

    $tabs.filter('.active').prev('li').find('a[data-toggle="tab"]').tab('show');

    $tabs.filter('.active').next('li').find('a[data-toggle="tab"]').each(function () {
        $(this).attr("data-toggle", "").parent('li').addClass("disabled");        
    })
}

function showNext() {
    $tabs.filter('.active').next('li').removeClass("disabled");
    $tabs.filter('.active').next('li').find('a[data-toggle]').each(function () {
        $(this).attr("data-toggle", "tab");
    });

    $tabs.filter('.active').next('li').find('a[data-toggle="tab"]').tab('show');

    $tabs.filter('.active').prev('li').find('a[data-toggle="tab"]').each(function () {
        $(this).attr("data-toggle", "").parent('li').addClass("disabled");;        
    })
}

If you are having multiple tabs, set class="disabled"and data-toggle=""for all the li items that you want to deactivate.

如果您有多个选项卡,请为所有要停用的 li 项目设置class="disabled"data-toggle=""

回答by user2249160

try this for disabling a tab

试试这个来禁用选项卡

$('#tab').attr('class', 'disabled');

    $('tab').click(function(event){
        if ($(this).hasClass('disabled')) {
            return false;
        }
    });

回答by mikeh

I was having the same issue and this is what i used.

我遇到了同样的问题,这就是我使用的。

$("element").click(event) {
    event.stopImmediatePropagation();
}

you just intercept the click event and then kill the event. To show the tabs, I'd just use $(tab.content).fadeIn();

您只需拦截点击事件,然后终止该事件。要显示选项卡,我只需使用 $(tab.content).fadeIn();

回答by dkimbell13

Super simple...

超级简单..

Add disabled class to tab li.

on show.bs.tab event attached to a[data-toggle='tab'] element 
check to see if its parent has the disabled class...

if so return false...

回答by Danilo Colasso

$(document).ready(function(){
    $("#tabs > li").click(function(){
        if($(this).hasClass("disabled"))
            return false;
    });
});

It will be enough to work

工作就够了

回答by Subhasom

Use following code to disable the user Click on tab:

使用以下代码禁用用户单击选项卡:

$('li:has([data-toggle="tab"])').click(function(event){return false;});

Maker sure this code will be added after your bootsrtap js

确保在 bootsrtap js 之后添加此代码

回答by Iexist

I, had this problem for a while.

我,这个问题有一段时间了。

Remove the data-toggle = ..

删除数据切换 = ..

to do conditionnaly by javascript Give tabs, Ids in the tag document.getElementById(tabId)[0].setAttribute("data-toggle", "");

用javascript做conditionnaly在标签document.getElementById(tabId)[0].setAttribute("data-toggle", "");

回答by Poorya

I used

我用了

style="pointer-events: none;"

something like:

就像是:

<div class="steps nav" role="tablist">
    <div href="#first" data-toggle="tab" role="tab" class="step mb-4 active" style="pointer-events: none;">
        <div class="number">1</div>
        first
    </div>
    <div href="#second" data-toggle="tab" role="tab" class="step mb-4" style="pointer-events: none;">
        <div class="number">2</div>
        second
    </div>
</div>

seems to be doing the trick. still able to perform navigation using .click() function. which is what most are looking for.

似乎在做伎俩。仍然可以使用 .click() 函数执行导航。这是大多数人正在寻找的。

something like

就像是

$('.nav [href="' + target + '"]').click()

Happy coding!

快乐编码!

回答by Abou-Emish

I tried all suggested answers, but finally i made it work like this

我尝试了所有建议的答案,但最后我让它像这样工作

if (AnyCondition) //your condition
{
    $("a[data-toggle='tab'").prop('disabled', true);
    $("a[data-toggle='tab'").each(function () {
        $(this).prop('data-href', $(this).attr('href')); // hold you original href
        $(this).attr('href', '#'); // clear href
    });                
    $("a[data-toggle='tab'").addClass('disabled-link');
}
else
{
    $("a[data-toggle='tab'").prop('disabled', false);
    $("a[data-toggle='tab'").each(function () {
        $(this).attr('href', $(this).prop('data-href')); // restore original href
    });
    $("a[data-toggle='tab'").removeClass('disabled-link');
}
// if you want to show extra messages that the tab is disabled for a reason
$("a[data-toggle='tab'").click(function(){
   alert('Tab is disabled for a reason');
});