【问题标题】:JQuery Tab not hidingJQuery选项卡没有隐藏
【发布时间】:2012-07-11 20:15:31
【问题描述】:

我正在尝试让 JQuery 选项卡(不是 UI)在创建动态选项卡和从 php 文件加载动态内容时播放得很好。 php 文件返回一些表格、css 和图像。一切都按预期加载。我正在尝试通过 href 链接创建其他选项卡。一切似乎都在工作,除了创建的初始默认选项卡内容容器在创建其他选项卡或单击以激活时不会隐藏。相反,附加选项卡的内容会添加到初始内容容器的底部。如果我用简单的“Hello World”替换我的 php 文件,一切正常。我已经梳理了 php 数据,并且 html 中没有冲突的元素名称。没什么花哨的,只有 html、css 和图像。

您可以查看示例演示here

这是我的 js:

$(function() {
    var total_tabs = 0;
    var pId = 0;
    var pName = "";

    //initialize first tab
    addtab(total_tabs,pId,"Server Details");

    $("a.tb_showTab").click(function() {
        total_tabs++;
        var vId = $(this).attr('hash').replace('#','');
        var pValues = vId.split('|');               
        $("#tabcontent p").hide();
        addtab(total_tabs,pValues[1],pValues[0]);
        return false;
    });

    function addtab(count,pId,pName) {
        var closetab = '<a href="" id="close'+count+'" class="close">&times;</a>';
        //no close button on default tabs
        if (pId==0 || pId==1){closetab = ""};

        //Create Tab
        $("#tabul").append('<li id="t'+count+'" class="ntabs">'+pName+'&nbsp;&nbsp;'+closetab+'</li>');

        //Create Tab Content
        if (pId==0){
            //load Server Details
            $.get("test5.php",
                {nbRandom: Math.random() },
                function(data){
                    $("#tabcontent").append('<p id="c'+count+'">'+data+'</p>');
                });
            }
        else if (pId==1){
            //load Groups Details
            //eventually replace this with dynamic content from php file
            $("#tabcontent").append('<p id="c'+count+'">'+pName+' content goes here!!</br>ID='+pId+'</p>');
            }
        else {
            //load Person Details
            //eventually replace this with dynamic content from php file
            $("#tabcontent").append('<p id="c'+count+'">'+pName+' content goes here!!</br>ID='+pId+'</p>');
            }
        $("#tabul li").removeClass("ctab");
        $("#t"+count).addClass("ctab");

        $("#t"+count).bind("click", function() {
            $("#tabul li").removeClass("ctab");
            $("#t"+count).addClass("ctab");
            $("#tabcontent p").hide();
            $("#c"+count).fadeIn('slow');
        });

        $("#close"+count).bind("click", function() {
            // activate the previous tab
            $("#tabul li").removeClass("ctab");
            $("#tabcontent p").hide();
            $(this).parent().prev().addClass("ctab");
            $("#c"+count).prev().fadeIn('slow');

            $(this).parent().remove();
            $("#c"+count).remove();
            return false;
        });
    }
});

这是html:

<a class="tb_showTab" href="#Tab_Name_1|11111" >Add Tab1</a>&nbsp;&nbsp;|&nbsp;&nbsp;
<a class="tb_showTab" href="#Tab_Name_2|22222" >Add a Tab2</a>&nbsp;&nbsp;|&nbsp;&nbsp;
<a class="tb_showTab" href="#Tab_Name_3|33333" >Add a Tab3</a>    

<div id="container">
    <ul id="tabul">
        <li id="litab" class="ntabs add"></li>
    </ul>            
<div id="tabcontent"></div>
</div>

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript jquery jquery-tabs


    【解决方案1】:

    tabcontent 中的&lt;p&gt;&lt;/p&gt; 没有嵌套&lt;center&gt;&lt;/center&gt; 标记,实际内容在该标记中。试试下面的 js 代码——这似乎可行。

    $(function() {
    var total_tabs = 0;
    var pId = 0;
    var pName = "";
    
    //initialize first tab
    addtab(total_tabs,pId,"Server Details");
    
    $("a.tb_showTab").click(function() {
        total_tabs++;
        var vId = $(this).attr('hash').replace('#','');
        var pValues = vId.split('|');               
        $("#tabcontent center").hide();
        addtab(total_tabs,pValues[1],pValues[0]);
        return false;
    });
    
    function addtab(count,pId,pName) {
        $("#tabcontent p").hide();
        var closetab = '<a href="" id="close'+count+'" class="close">&times;</a>';
        //no close button on default tabs
        if (pId==0 || pId==1){closetab = ""};
    
        //Create Tab
        $("#tabul").append('<li id="t'+count+'" class="ntabs">'+pName+'&nbsp;&nbsp;'+closetab+'</li>');
    
        //Create Tab Content
        if (pId==0){
            //load Server Details
            $.get("test5.php",
                {nbRandom: Math.random() },
                function(data){
                    $("#tabcontent").append('<p id="c'+count+'">'+data+'</p>');
                });
            }
        else if (pId==1){
            //load Groups Details
            //eventually replace this with dynamic content from php file
            $("#tabcontent").append('<p id="c'+count+'">'+pName+' content goes here!!</br>ID='+pId+'</p>');
            }
        else {
            //load Person Details
            //eventually replace this with dynamic content from php file
            $("#tabcontent").append('<p id="c'+count+'">'+pName+' content goes here!!</br>ID='+pId+'</p>');
            }
        $("#tabul li").removeClass("ctab");
        $("#t"+count).addClass("ctab");
    
        $("#t"+count).bind("click", function() {
            $("#tabul li").removeClass("ctab");
            $("#t"+count).addClass("ctab");
            $("#tabcontent center").hide();
            $("#c"+count).fadeIn('slow');
        });
    
        $("#close"+count).bind("click", function() {
            // activate the previous tab
            $("#tabul li").removeClass("ctab");
            $("#tabcontent p").hide();
            $(this).parent().prev().addClass("ctab");
            $("#c"+count).prev().fadeIn('slow');
    
            $(this).parent().remove();
            $("#c"+count).remove();
            return false;
        });
    }
    });
    

    请注意,基本上,我只是将$("#tabcontent p") 部分更改为$("#tabcontent center")。如果这不是您想要的 html 输出,您可能需要再次查看您的 html。

    【讨论】:

    • 我已经按照您对我的在线示例的建议进行了更改,当我创建一个新选项卡时它确实隐藏了初始内容,但是第二个和第三个选项卡的内容只是附加到底部以前的内容。如果您尝试单击每个选项卡,则内容不会隐藏或重新出现。我不需要中心标签,它们只是为了测试链接的中心而消失。
    • @GSMACK,我已经编辑了我的答案。检查编辑历史。另外,我注意到没有将点击绑定到选项卡以显示其内容的功能。尝试打开所有选项卡并单击其他选项卡,而不关闭它们。内容不变。
    • addtab 函数在创建选项卡时包含一个#close+count 绑定,以处理每个选项卡的点击。在tabs.html 的在线示例中,我删除了中心标签并将所有内容更改回“#tabcontent p”.hide。我还将 .php 文件更改为仅返回 1 个表以限制空间。您会注意到,如果您添加所有选项卡,然后分别单击它们,选项卡 1、2、3 在单击选项卡时都会替换彼此的内容。它只是第一个不隐藏的初始选项卡。
    • 另外,我创建了tabs2.html 并替换了 php 文件的 .get 以仅显示文本,一切正常。看来从 php 文件加载的 css 和表正在破坏代码。如果您查看test7.php 的来源,您会发现没有标记错误,没有元素名称冲突,只有标准的 css 和表格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2010-11-19
    • 2018-07-17
    • 2013-01-22
    • 1970-01-01
    相关资源
    最近更新 更多