Javascript hide/show tabs using JQuery

余生长醉 提交于 2019-12-11 13:49:09

问题


I have a quick question of how I can use jQuery tabs (you click on link button to display/hide certain divs). The div id matches the href of the link:

HTML links:

<table class='layout tabs'>
<tr>
  <td><a href="#site">Site</a></td>
  <td><a href="#siteno">Number</a></td>
</tr>
<tr>
  <td><a href="#student">Student</a></td>

  <td><a href="#school">School</a></td>
</tr>
</table>
</div>

div that needs to display/hide:

<div id="site">
  <table class='explore'>
    <thead class='ui-widget-header'>
      <tr>
        <th class=' sortable'>
          Site
        </th>

        <th class=' sortable'>
          Number
        </th>
        </tr>
        </thead>
        </table>
</div>

回答1:


$("table.tabs a").click( function() {
    var id = $(this).attr( "href" );
    var div = $(id);
    div.toggle();
} );

This will get you exactly what you're asking. However, I suspect that you also want to hide all other divs when one div is shown. True?

Ok, now that you've responded that it's true, here's your new code. You also should add a class (in my code - "tab-div") to all your DIVs, in order to have them easily selectable all together.

$("table.tabs a").click( function() {
    var id = $(this).attr( "href" );

    // Hide all the tab divs
    $(".tab-div").hide(); 

    // Then show the one that's been clicked
    $(id).show();
} );


来源:https://stackoverflow.com/questions/2952020/javascript-hide-show-tabs-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!