简体   繁体   English

Django + Jquery +选项卡,单击时不显示选项卡

[英]Django + Jquery + Tabs, tab not displaying on click

I'm trying to set a javascript value inside a function when the function is passed onClick, then I want my function to take the id of the clicked target save it to a var, so I can call .show on that object based on its returned javascript var id. 当函数传递onClick时,我试图在函数内设置一个javascript值,然后我希望函数采用被单击目标的ID并将其保存到var中,因此我可以基于其对象在该对象上调用.show返回的javascript var id。

This is how I output the list links: 这是我输出列表链接的方式:

{%for stat in ip_new_list %}                        
    {% if 'Newip' in stat%}
        <li class="active"><a href onClick="showTable();" id='{{stat.1}}'>{{stat.1}} </a></li>
    {%endif%}
{%endfor%}                              
</ul> 

I hide all the tabs with a class name using the following code: 我使用以下代码隐藏了所有带有类名称的选项卡:

$(document).ready(function(){
        $('#loading').hide();
        $('#helppage').hide();
        $('.iptable').hide();  
    });

This is my code to show the div tag with the id of the clicked link, I have verified the div tag ids match the link ids which are '100.000.000.000' ip addresses in that format. 这是我的代码,用于显示带有单击链接的ID的div标签,我已验证div标签ID与该格式的链接ID(即“ 100.000.000.000” ip地址)匹配。

function showTable(clicked){
    console.log("ShowTableClicked");
    console.log(clicked.id);
    var clickedID = "#" + clicked.id;
    $(clickedID).show();
    console.log(clickedID);
}

Here is my django / html code: 这是我的django / html代码:

<ul>
{%for stat in ip_new_list %}                        
    {% if 'Newip' in stat%}
        <li class="active" id={{stat.1}}><a href='#' onClick="showTable(this);" id={{stat.1}}>{{stat.1}}</a></li>
    {%endif%}
{%endfor%}                              
</ul> 


{%for stat in ip_new_list %}
        {% if 'Break' in stat%}
                </div> <div id="{{stat.1}}"  class='iptable'>
        {%endif%}

        {% if 'First' in stat%}
                <div id="{{stat.1}}" class='iptable'>
        {%endif%}


            {{stat.0}} | {{stat.1}} | {{stat.2}} <br>

{%endfor%}  

Here is the console output: 这是控制台输出:

"ShowTableClicked" ipreport:56
"000.000.000.000" ipreport:57
"#000.000.000.000"

but the div tag still doesn't appear I used debug and inspector to verify the div tag is there with the correct ID. 但是div标签仍然没有出现,我使用了调试和检查器来验证div标签是否具有正确的ID。 Please let me know if you can figure out why. 如果您能找出原因,请告诉我。

Your problem is in the jQuery selector line - $("#" + clickedID).show(); 您的问题出在jQuery选择器行中- $("#" + clickedID).show();

Selecting an element in jQuery by ID (and CSS, btw) starts with # . 通过ID(和CSS,btw)在jQuery中选择元素的开始是#

More info here: http://api.jquery.com/id-selector/ 此处提供更多信息: http : //api.jquery.com/id-selector/

Here is the correct working answer. 这是正确的工作答案。 I had to add link to the id of the link because it wasn't finding the div tag because they both had that id and it was changing the style or both, so in order to solve that problem I just attached the word link and removed with .replace this works quite well. 我必须将链接添加到链接的ID中,因为找不到div标签是因为它们都具有该ID,并且正在更改样式或同时更改两者,因此,为了解决该问题,我仅附加了单词link并删除了用.replace可以很好地工作。 Now its time to style and use fade instead of show. 现在是时候设置样式并使用淡入淡出而不是显示了。

Thanks for all your help. 感谢你的帮助。

{%for stat in ip_new_list %}                        
    {% if 'Newip' in stat%}
        <a href='#' onClick="showTable(this);" id="{{stat.1}}link">{{stat.1}}</a>
    {%endif%}
{%endfor%}                              



<div class="table-responsive">
{%for stat in ip_new_list %}
        {% if 'Break' in stat%}
                </tbody></table></div> <div id="{{stat.1}}" class='iptable'> <table class="table" id='table-data'> <tbody>
        {%endif%}   
        {% if 'First' in stat%}
                <div id="{{stat.1}}" class='iptable'><table class="table" id='table-data'><tbody>
        {%endif%}       

        {% if 'Break' in stat%}
        {%else%}
            {% if 'First' in stat%}
                {%else%}
            <tr>
                <td>{{stat.0}}</td>
                <td>{{stat.1}}</td>
                <td>{{stat.2}}</td>         
            </tr>
        {%endif%}
        {%endif%}

{%endfor%} 

</div>

At the bottom of the page added the script and changed it to this. 在页面底部添加了脚本并将其更改为此。

<script>


function showTable(clicked){
    $(".iptable").hide();
    var id = clicked.id
    id = id.replace('link', '');
    console.log(id);
    document.getElementById(id).style.display = "block";
}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM