简体   繁体   English

在li上单击切换特定表

[英]On li click toggle a specific table

In my code i have 2 divs one div contains 3 table with id table1, table2 , table3. 在我的代码中,我有2个div,一个div包含ID为table1,table2,table3的3个表。 Other div has 3 li elements whose class is same as table id. 其他div有3个li元素,它们的类与表id相同。 first li class is table1, second li class is table2... and so on. 第一个li类是table1,第二个li类是table2 ...依此类推。 Initially all the tables are hide. 最初,所有表都是隐藏的。 On first li click i want to toggle first table, on second toggle to second table... and so on. 在第一个li上单击我要切换第一个表,在第二个上切换到第二个表...等等。 The HTML code is HTML代码是

    <div class="allTemplateName">
        <li class="table1">A</li>
        <li class="table2">B</li>
        <li class="tbale3">C</li>
    </div>

 <div class="container">
   <table id="table1">
   <tr>
     <td> Hello</td>
   </tr>
  </table>
<table id="table2">
   <tr>
   <td> Hello</td>
    </tr>
   </table>
   <table id="table3">
    <tr>
     <td> Hello</td>
   </tr>
   </table>
 </div>

I am using this j query code to get the result but it is not working. 我正在使用此j查询代码来获取结果,但是它不起作用。

    $(document).ready(function() {
      $(".allTemplateName li").click(function() {
       // get the target table:
       var tarTable = $("#" + $(this).html());

            // toggle:
         tarTable.toggle();

           $('.table').not(tarTable).hide();
        });
    });

Few changes : 几处变化:

  • .html() gets you inner html of the element,use .attr('class') or prop('class') to get the class Name. .html()获取元素的内部html,使用.attr('class')prop('class')获得类名。
  • $('.table') will find elements with class as tablr . $('.table')将查找类为tablr的元素. - class selector. -类选择器。 Either add class attribute as table or use $('table') element selector. 可以将class属性添加为table也可以使用$('table')元素选择器。
  • Typo in the class attribute <li class="tbale3">C</li> - class="table3" 类别属性<li class="tbale3">C</li>中的错字<li class="tbale3">C</li> class="table3"

$(document).ready(function () {
    $(".allTemplateName li").click(function () {
        // get the target table:
        var tarTable = $("#" + $(this).attr('class'));

        // toggle:
        tarTable.toggle();    
        //if you need to keep the element visible use tarTable.show()

        $('.container table:visible').not(tarTable).hide();
    });
});

Fiddle 小提琴

You're close, but you don't want the HTML of the li , you want its class (and I suggest changing that, below). 您已经接近了,但是您不想要liHTML ,而是想要它的class (我建议在下面进行更改)。

$(function() {
    $(".allTemplateName li").click(function() {
        var tableSelect = "#" + $(this).attr("class");
        var tables = $(".container table");
        tables.filter(tableSelect).toggle();
        tables.not(tableSelect).hide();
    });
});

The reason I wouldn't use class is that you may well want to add other classes to those li elements, which would break it. 我不使用class的原因是,您可能希望向这些li元素添加其他类,这会破坏它。 Instead, I'd use a data-* attribute: 相反,我将使用data-*属性:

<div class="allTemplateName">
    <li data-table="#table1">A</li>
    <li data-table="#table2">B</li>
    <li data-table="#table3">C</li>
</div>

Then: 然后:

$(function() {
    $(".allTemplateName li").click(function() {
        var tableSelect = $(this).attr("data-table");
        var tables = $(".container table");
        tables.filter(tableSelect).toggle();
        tables.not(tableSelect).hide();
    });
});

Live Example: 现场示例:

 $(".container table").hide(); $(function() { $(".allTemplateName li").click(function() { var tableSelect = $(this).attr("data-table"); var tables = $(".container table"); tables.filter(tableSelect).toggle(); tables.not(tableSelect).hide(); }); }); 
 <div class="allTemplateName"> <li data-table="#table1">A</li> <li data-table="#table2">B</li> <li data-table="#table3">C</li> </div> <div class="container"> <table id="table1"> <tr> <td>Table 1</td> </tr> </table> <table id="table2"> <tr> <td>Table 2</td> </tr> </table> <table id="table3"> <tr> <td>Table 3</td> </tr> </table> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

Please make the structure like below code... 请使结构类似于以下代码...

<div class="allTemplateName">
   <li data-id="table1" class="template-name">A</li>
   <li data-id="table2" class="template-name">B</li>
   <li data-id="table3" class="template-name">C</li>
</div>

<div class="container">
  <table id="table1" class="table-select">
   <tr>
    <td> Hello</td>
   </tr>
  </table>
  <table id="table2" class="table-select">
   <tr>
    <td> Hello</td>
   </tr>
  </table>
  <table id="table3" class="table-select">
   <tr>
    <td> Hello</td>
   </tr>
  </table>
</div>

Then use the jquery like this: 然后像这样使用jQuery:

$("body").on("click", ".template-name", function (e) {
   var tableId = $(this).attr("data-id");
$(".table-select").hide(); // this would be hide all table initially 
$("#"+tableId).show();// this will be show selected table contains unique id 
 });

you can use .index(); 您可以使用.index(); without need to Classes or Ids .. simply if you click on 1st li show 1st table .. and if click 2nd li show 2nd table .. so on... 只需点击第一个li显示第一个表..如果单击第二个li显示第二个表..等等,就不需要类或ID。

 $(document).ready(function() {
      $(".allTemplateName li").on('click',function() {
       // get the target table:
       $('.container table').hide();
       $('.container table').eq($(this).index()).slideDown();
    });
});

JSFIDDLE 的jsfiddle

Try this : I've also added some CSS to it. 试试这个:我还添加了一些CSS。

Here is the working fiddle : http://jsfiddle.net/dLz9cweh/ 这是工作的小提琴: http : //jsfiddle.net/dLz9cweh/

HTML : HTML:

<div class="allTemplateName">
    <li class="table1">A</li>
    <li class="table2">B</li>
    <li class="table3">C</li>
</div>
<div class="container">
    <table id="table1" class="hide">
        <tr>
            <td>Hello A</td>
        </tr>
    </table>
    <table id="table2" class="hide">
        <tr>
            <td>Hello B</td>
        </tr>
    </table>
    <table id="table3" class="hide">
        <tr>
            <td>Hello C</td>
        </tr>
    </table>
</div>

CSS : CSS:

table {
    border:1px solid black;
    margin:10px;
}
li {
    cursor:pointer;
}
.hide {
    display : none;
}
.active {
    display:block
}

JS JS

$(document).ready(function () {
     $(".allTemplateName li").click(function () {
         var id = $(this).attr('class');
         $("#" + id).addClass('active').siblings().removeClass('active');

     });
 });

html: HTML:

<div class="allTemplateName">
<li class="table1">A</li>
<li class="table2">B</li>
<li class="table3">C</li>
</div>
<div class="container">
<table id="table1">
    <tr>
        <td> Hello 1</td>
    </tr>
</table>
<table id="table2">
    <tr>
        <td> Hello 2</td>
    </tr>
</table>
<table id="table3">
    <tr>
        <td> Hello 3</td>
    </tr>
</table>

js: JS:

$(document).ready(function () {
    $(".allTemplateName li").click(function () {
        // get the target table:
        var tarTable = $("#" + $(this).attr('class'));
        // toggle:
        tarTable.toggle();

        $('.table').not(tarTable).hide();
    });
});

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

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