【问题标题】:Disable un-selected tab禁用未选择的选项卡
【发布时间】:2013-06-27 05:47:31
【问题描述】:

我有一个标签菜单,这是我的 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>

【问题讨论】:

  • 你遇到了什么问题??
  • 请使用 JS fiddle :)
  • 那你有什么疑问?
  • @Vivek-我想禁用所有其他标签

标签: php javascript jquery menu tags


【解决方案1】:
<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>

【讨论】:

    【解决方案2】:

    点击后这个unbindclick事件怎么样

    <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>
    

    【讨论】:

    • 我在每个选项卡中都有按钮,我只想在单击当前选项卡上的按钮时启用下一个选项卡...
    • 显示你的 html 有哪些按钮,在哪个按钮上打开哪个选项卡??
    【解决方案3】:

    试试这个:

    <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>
    

    【讨论】:

    • 感谢您的回复,我编辑了,但我仍然面临同样的问题。
    • @KiranRs - 您的 js 控制台中是否收到任何警告/错误?
    • 尝试更新版本。它现在针对与 jquery 的绑定/取消绑定版本的实际链接。希望对您有所帮助。
    【解决方案4】:

    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");
            }
        });
    });
    

    沃金演示http://jsfiddle.net/cse_tushar/YZjJa/

    新建 js--> 按钮点击进入下一个标签

    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/

    【讨论】:

    • 它的工作朋友,但我在每个标签中都有一个按钮,当用户单击当前标签时我想转到下一个标签。
    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多