首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript在单击按钮时隐藏某些表行

Javascript在单击按钮时隐藏某些表行
EN

Stack Overflow用户
提问于 2013-02-25 15:18:39
回答 1查看 9.4K关注 0票数 1

我想在单击按钮时隐藏表格中的某些行,同时保留某些行。例如,我点击了"Show Fire-based champs“按钮。它将只显示具有Fire属性的冠军,并将隐藏其余的(来自同一个表)。我点击了另一个名为“显示基于水的冠军”的按钮,它将只显示具有水属性的冠军,并将隐藏其余的属性等,并将相同的其他按钮。任何人都可以为它编写代码吗?我没有JS的经验。

下面是主表的外观(这只是一个示例,表行要多得多)。

代码语言:javascript
复制
<table border="1" width="800">
<thead><tr>
    <th>Hero</th>
    <th>Class</th>
    <th>Offensive Skill</th>
    <th>Passive Skill</th>
    <th>Ultimate</th>
</tr></thead>
<tbody>
    <tr><td><a class="holy">Arcana</a></td>
    <td>the Arcane Manipulator</td>
    <td> - </td>
    <td> - </td>
    <td>Arcane Destroyer</td></tr>

    <tr><td><a class="fire">Azakiel</a></td>
    <td>the Blood Mage</td>
    <td> - </td>
    <td>Elf Blood</td>
    <td>Call of the Phoenix</td></tr>

    <tr><td><a class="wind">Bahamut</a></td>
    <td>the King of the Skies</td>
    <td> - </td>
    <td> - </td>
    <td>Mega Flare</td></tr>

    <tr><td><a class="dark">Carinblack</a></td>
    <td>the Dark Assailant</td>
    <td>Sword Bash</td>
    <td> - </td>
    <td>Blade of the Dark</td></tr>

    <tr><td><a class="earth">Dran</a></td>
    <td>the Steel Beast</td>
    <td>Rushing Tackle</td>
    <td> - </td>
    <td>Rolling Thunder</td></tr>

    <tr><td><a class="water">Fenrir</a></td>
    <td>the Water Emperor</td>
    <td>Water Barrage</td>
    <td> - </td>
    <td>Waterfall</td></tr>

    <tr><td><a class="thunder">Larza</a></td>
    <td>the Lightning Heroine</td>
    <td>Staff of Lightning</td>
    <td> - </td>
    <td>Storm Surge</td></tr>

    <tr><td><a class="thunder">Razor</a></td>
    <td>the Thunder Emperor</td>
    <td>Thunder Strike</td>
    <td> - </td>
    <td>Thunderstorm</td></tr>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-25 15:22:04

使用show()hide()

例如,当点击"Show fire-based champs“时,执行以下操作:

代码语言:javascript
复制
$('tbody tr').hide() //Hide all rows
$('tbody tr:has(a.fire)').show() //Show all fire rows

Here is a jsfiddle that has all the code in it

HTML代码:

代码语言:javascript
复制
<html><head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.0b1.js"></script>
<script type="text/javascript">

 function show(champ){
  $('tbody tr').hide()   
  $('tbody tr:has(a.'+champ+')').show()
 }


</script>


</head>
<body>
  <table border="1" width="800">
<thead><tr>
    <th>Hero</th>
    <th>Class</th>
    <th>Offensive Skill</th>
    <th>Passive Skill</th>
    <th>Ultimate</th>
</tr></thead>
<tbody>
    <tr><td><a class="holy">Arcana</a></td>
    <td>the Arcane Manipulator</td>
    <td> - </td>
    <td> - </td>
    <td>Arcane Destroyer</td></tr>

    <tr><td><a class="fire">Azakiel</a></td>
    <td>the Blood Mage</td>
    <td> - </td>
    <td>Elf Blood</td>
    <td>Call of the Phoenix</td></tr>

    <tr><td><a class="wind">Bahamut</a></td>
    <td>the King of the Skies</td>
    <td> - </td>
    <td> - </td>
    <td>Mega Flare</td></tr>

    <tr><td><a class="dark">Carinblack</a></td>
    <td>the Dark Assailant</td>
    <td>Sword Bash</td>
    <td> - </td>
    <td>Blade of the Dark</td></tr>

    <tr><td><a class="earth">Dran</a></td>
    <td>the Steel Beast</td>
    <td>Rushing Tackle</td>
    <td> - </td>
    <td>Rolling Thunder</td></tr>

    <tr><td><a class="water">Fenrir</a></td>
    <td>the Water Emperor</td>
    <td>Water Barrage</td>
    <td> - </td>
    <td>Waterfall</td></tr>

    <tr><td><a class="thunder">Larza</a></td>
    <td>the Lightning Heroine</td>
    <td>Staff of Lightning</td>
    <td> - </td>
    <td>Storm Surge</td></tr>

    <tr><td><a class="thunder">Razor</a></td>
    <td>the Thunder Emperor</td>
    <td>Thunder Strike</td>
    <td> - </td>
    <td>Thunderstorm</td></tr>
    </tbody>
</table>
<button onclick="show(&quot;fire&quot;)">Show fire</button>
<button onclick="show(&quot;water&quot;)">Show water</button>
<button onclick="show(&quot;thunder&quot;)">Show thunder</button>
<button onclick="show(&quot;dark&quot;)">Show dark</button>
<button onclick="show(&quot;earth&quot;)">Show earth</button>
<button onclick="show(&quot;holy&quot;)">Show holy</button>
<button onclick="show(&quot;wind&quot;)">Show wind</button>
<button onclick="$('tbody tr').show()  ">Show all</button>
</body></html>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15061891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档