3

我有一个 jquery 选项卡容器,其中有两个选项卡。现在我想要的是最初的第二个选项卡将被禁用。单击第一个选项卡上的按钮时,将启用并自动打开第二个选项卡。

<script type="text/javascript">
    $(document).ready(function() {
    //When page loads...
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content
    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
            $(this).addClass("active"); //Add "active" class to selected tab
            $(".tab_content").hide(); //Hide all tab content
            var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
            $(activeTab).fadeIn(); //Fade in the active ID content
            return false;
        });

      });
</script>

<div class="tab-wrapper" id="tab-wrapper">
    <div class="tab-header">
        <ul class="tabs">
                <li><a href="#tab1">overview</a></li>
                <li><a href="#tab2">details</a></li>
            </ul>
    </div>
    <div class="tab_container">
            <div id="tab1" class="tab_content">
                here is the list of the overview
        </div>
            <div id="tab2" class="tab_content">
                Particular Details
            </div>
        </div>
</div>
4

4 回答 4

5

您可以使用以下

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

并使用零索引数字来选择禁用哪些。要启用您的选项卡,您可以使用以下命令:

$("#tabs").tabs({disabled: false });

要设置选定的选项卡,您可以使用jQuery Tabs中的此示例:

var $tabs = $('#tabs').tabs(); // first tab selected

$('#my-text-link').click(function() { // bind click event to link
    $tabs.tabs('select', 2); // switch to third tab
    return false;
});
于 2012-10-09T00:30:59.597 回答
2

我会默认禁用所有这些,然后启用第一个。之后,按钮应该非常简单。所有这些都应该非常简单。

第一:jQuerystabs('disable')只是不起作用。所以我建立了自己的功能来禁用它们。这里是:

function disableTabs(obj){
    var c = obj.find("ul li").size();
    for(var i=0;i<c;i++){
        obj.tabs("disable", i);
    }
}

你可以像这样使用它:disableTabs($('#myTabs')). jQuery 不允许您禁用当前选项卡 - 由于这是页面加载,因此它将是第一个选项卡。然后,您将需要创建一些变量来存储一些数据......

var openTab = 0;
var currTab = 0;

然后,有几个函数来处理它......

function enableTab(obj, num){
    obj.tabs("enable", num);
}
function next(){
    openTab++;
    currTab++;
    enableTab($(".tabs"), currTab);
    $(".tabs").tabs("select", currTab);
}
function prev(){
    openTab--;
    currTab--;
    $(".tabs").tabs("select", currTab);
}

然后,我们只需附加一些事件处理程序:

$('#myTab').bind('tabsshow', function(event, ui) {
    currTab = ui.index;
});
$(".next").click(function(){
    next();
});
$(".prev").click(function(){
    prev();
});

这些按钮的 HTML 非常简单:

<a class='prev'>Previous</a>
<a class='next'>Next</a>

希望这会有所帮助——祝你好运!

顺便说一句:我这里有一些完整的工作标签,你可能想看看。

于 2011-04-14T14:05:42.007 回答
0

假设您正在单击一个不是内部选项卡类的按钮并希望打开第二个选项卡,那么您可以添加触发事件,例如:

$('.tabs > #tab2').trigger('click');

并将 id 赋予 li > a :<a id="tab1" href="#tab1"></a>

于 2011-04-14T12:27:40.693 回答
0

我解决这个问题的方法是使用一个变量来跟踪按钮是否被点击,默认情况下将其设置为“false”,当按钮被点击时设置为“true”。

在选项卡链接的单击事件中检查此变量的值将允许您决定是否显示新选项卡、什么也不做,或者可能显示一条消息。

这是一个工作示例:http: //jsbin.com/ijoco5

于 2011-04-14T10:43:55.520 回答