禁用未选中的选项卡

时间:2013-06-27 05:47:31

标签: php javascript jquery menu tags

我有一个标签菜单,这是我的HTML代码,

<li class="active"><a href="#tab1">Tab 1</a></li>
            <li><a class="icon_accept" href="#tab2">Tab with icon</a></li>
            <li><a href="#tab3">Long name for the last tab</a></li>
        </ul>
    </div>
    <div id="tabs_content_container">
        <div id="tab1" class="tab_content" style="display: block;">
            <p>content 1</p>
        </div>
        <div id="tab2" class="tab_content">
            <p>content 2</p>
        </div>
        <div id="tab3" class="tab_content">
            <p>content 3</p>
        </div>

但是当我选择一个标签时,我想要禁用所有其他标签,这意味着当一个标签处于活动状态时,没有人可以点击任何其他标签菜单!

这是我的java脚本。

<script type="text/javascript">

$(document).ready(function(){
    $("#tabs li").click(function() {
        //    First remove class "active" from currently active tab
        $("#tabs li").removeClass('active');

        //    Now add class "active" to the selected/clicked tab
        $(this).addClass("active");

        //    Hide all tab content
        $(".tab_content").hide();

        //    Here we get the href value of the selected tab
        var selected_tab = $(this).find("a").attr("href");

        //    Show the selected tab content
        $(selected_tab).fadeIn();

        //    At the end, we add return false so that the click on the link is not executed
        return false;
    });
});

</script>

4 个答案:

答案 0 :(得分:0)

试试这个:

<script type="text/javascript">

    $(document).ready(function(){
        $("#tabs li").click(function() {
            //    First remove class "active" from currently active tab
            $("#tabs li").removeClass('active');

            $("#tabs li").find('a').unbind('click', false);
            //OR $("#tabs li a").unbind('click', false);

            //    Now add class "active" to the selected/clicked tab
            $(this).addClass("active");
            $(this).find('a').bind('click', false);

            //    Hide all tab content
            $(".tab_content").hide();

            //    Here we get the href value of the selected tab
            var selected_tab = $(this).find("a").attr("href");

            //    Show the selected tab content
            $(selected_tab).fadeIn();

            //    At the end, we add return false so that the click on the link is not executed
            return false;
        });
    });

    </script>

答案 1 :(得分:0)

点击后unbind click事件怎么样?

<script type="text/javascript">

$(document).ready(function(){
    $("#tabs li").click(function() {
        //    First remove class "active" from currently active tab
        $("#tabs li").removeClass('active');

        //    Now add class "active" to the selected/clicked tab
        $(this).addClass("active");

        //    Hide all tab content
        $(".tab_content").hide();

        //    Here we get the href value of the selected tab
        var selected_tab = $(this).find("a").attr("href");

        //    Show the selected tab content
        $(selected_tab).fadeIn();

        //    At the end, we add return false so that the click on the link is not executed

 $("#tabs li").unbind("click"); 
        return false;
    });
});

</script>

答案 2 :(得分:0)

HTML

<<div id="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">
    <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollici\ tristique tempus lectus.</p>
    <input type="button" value="1" id="s1" class="tush" />
</div>
<div id="tabs-2">
    <p>Morbi tincidunt, dui sit amet f kh fwjh</p>
    <input type="button" value="2" id="s2" class="tush" />
</div>
<div id="tabs-3">
    <p id="hi">Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi n.</p>
    <input type="button" value="2" id="s3" class="tush" />
</div>

的jQuery

$(document).ready(function () {
    $('#tabs').tabs({
        collapsible: true,
        active: 10
    });
    $("#tabs li").click(function () {
        $("#tabs li a").unbind("click");
        return false;
    });
});

工作演示http://jsfiddle.net/cse_tushar/N2dn5/

的jQuery

$(document).ready(function () {
    $('#tabs').tabs({
        collapsible: true,
        active: 10,
        activate: function (event, ui) {
            $("#tabs li a").unbind("click");
        }
    });
});

Woking Demo http://jsfiddle.net/cse_tushar/YZjJa/

新js - &gt;按钮单击以转到下一个选项卡

<强>的jQuery

$(document).ready(function () {
    $('#tabs').tabs({
        collapsible: true,
        active: 10,
        activate: function (event, ui) {
            $("#tabs li a").unbind("click");
        },
        beforeActivate: function (event, ui) {
            $("#tabs li a").bind("click");
        }
    });
    $('.tush').click(function () {
        var tab_index = $('#tabs ul li.ui-state-active').attr('aria-controls');
        var lastChar = tab_index.substr(tab_index.length - 1)
        $('#tabs').tabs({
            active: lastChar
        });
        return false;
    });
});

工作演示http://jsfiddle.net/cse_tushar/YZjJa/1/

答案 3 :(得分:0)

<script type="text/javascript">

$(document).ready(function(){
    $("#tabs li").click(function() {
        //    First remove class "active" from currently active tab
        $("#tabs li").removeClass('active');

        $("#tabs li").find('a').unbind('click', false);
        //OR $("#tabs li a").unbind('click', false);

        //    Now add class "active" to the selected/clicked tab
        $(this).addClass("active");
        $(this).find('a').bind('click', false);

        //    Hide all tab content
        $(".tab_content").hide();

        //    Here we get the href value of the selected tab
        var selected_tab = $(this).find("a").attr("href");

        //    Show the selected tab content
        $(selected_tab).fadeIn();

        //    At the end, we add return false so that the click on the link is not executed
        return false;
    });
});

    </script>