Next-Prev functionality for the jquery tabs

2019-03-01 10:26发布

问题:

I need Next, Previous functionality for the jquery tabs on clicking on the Next and Prev html buttons. I am using jquery.1.9.1.js and jquery-ui-1.10.2.custom.js file. I have implemented below code but does not work for me.

<script language="javascript" type="text/javascript">
    $(function () {
        $("#ui-tabs").tabs();
        function GetSelectedTabIndex() {
            return $('#ui-tabs').tabs('option', 'selected');
        }

        function ShowTabs(stepNum) {
            var num = parseInt(stepNum);
            $('#ui-tabs').tabs('option', 'active', parseInt(GetSelectedTabIndex()) +  num);
        }

        $('#prev').click(function () {
            ShowTabs(-1);
        })

        $('#next').click(function () {
            ShowTabs(-1);
        })
    });
</script>

<div id="ui-tabs">
    <ul>
        <li><a href="#tabs-1">Nunc tincidunt</a></li>
        <li><a href="#tabs-2">Proin dolor</a></li>
        <li><a href="#tabs-3">Aenean lacinia</a></li>
    </ul>
    <div id="tabs-1">Tab1 content </div>
    <div id="tabs-2">Tab2 content </div>
    <div id="tabs-3">Tab3 content </div>
</div>
<a id="prev" class="button-style" href="#">Prev</a>
&nbsp;<a id="next" class="button-style" href="#">Next</a>

But GetSelectedTabIndex returns null. What is wrong in the implementation. Please suggest.

回答1:

There is no select method for jQuery UI Tabs in this version. To get your functionality to work you need to change your code to

var i=$('#ui-tabs').tabs( "option", "active"); //get selected tab index
$('#ui-tabs').tabs( "option", "active", i+num ); // num is your tab choise (+1,-1)

This is worked for me. Try this.



回答2:

This works for me:

$( "#ui-tabs" ).tabs();

 function GetSelectedTabIndex() {
        return $('#ui-tabs').tabs('option', 'selected');
 }

function ShowTabs(stepNum) {
    var num = parseInt(stepNum);
    $('#ui-tabs').tabs('option', 'active', parseInt(GetSelectedTabIndex()) + num);
}

It seems to me that there's no point in using the $tabs variable, as it's local to your GetSelectedTabIndex function, and it's only used once per function call...

Demo: http://jsfiddle.net/darkajax/A8ejN/



回答3:

<script src="jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="jquery-ui.js" type="text/javascript"></script>
<link href="jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(function () {
        $("#ui-tabs").tabs();
    });
    function GetSelectedTabIndex() {
        var $tabs = $('#ui-tabs').tabs();
        var selected = $tabs.tabs('option', 'selected');
        return selected;
    }

    function ShowTabs(stepNum) {
        var num = parseInt(stepNum);
        $('#ui-tabs').tabs('select', parseInt(GetSelectedTabIndex()) + num);
    }
</script>



<div id="ui-tabs">
    <ul>
        <li><a href="#tabs-1">Nunc tincidunt</a></li>
        <li><a href="#tabs-2">Proin dolor</a></li>
        <li><a href="#tabs-3">Aenean lacinia</a></li>
    </ul>
    <div id="tabs-1">
        Tab1 content
    </div>
    <div id="tabs-2" >
        Tab2 content
    </div>
    <div id="tabs-3">
        Tab3 content
    </div>
</div>