javascript - 更改特定标签的类别

标签 javascript jquery html css

我正在尝试将包含多个数据表的页面放在一起。由于页面可能会变长,我希望用户能够通过单击标题折叠表格。我计划使用 + 图标表示有更多内容可用,使用 - 图标表示它是可折叠的。所以我需要切换图标的 i 标签的类名。就像我说的,该页面可以包含多个具有相同 i tags 标签的表格,所以我需要一些东西来覆盖它

这是示例 HTML。

<table class="table table-bordered">
        <thead class="heading" id="thead">
      <tr id="tr">
         <th id="th" colspan="3"><i class="icon-plus"></i> Basic info</th>
      </tr>
    </thead>
    <tbody class="content">
        <tr>
          <td>Registration</td>
          <td>ABC 123</td>
          <td>&nbsp;</td>
        </tr>
        </tbody>
</table>

<table class="table table-bordered">
        <thead class="heading" id="thead">
      <tr id="tr">
         <th id="th" colspan="3"><i class="icon-plus"></i> More info</th>
      </tr>
    </thead>
    <tbody class="content">
        <tr>
          <td>Start time</td>
          <td>11:57</td>
          <td>&nbsp;</td>
        </tr>
        </tbody>
</table>

这是脚本

<script type="text/javascript">
jQuery(document).ready(function() {
  jQuery('.content').show();
  jQuery('.heading').click(function()
  {
    jQuery(this).next('.content').slideToggle('0');
    jQuery(this).parent().next("i").removeClass('icon-plus').addClass('icon-minus');
  });
});
</script>

tbody 的显示和隐藏工作正常,但我无法更改图标,有什么线索吗?

最佳答案

尝试改变

jQuery(this).parent().next("i").removeClass('icon-plus').addClass('icon-minus'); 

jQuery(this).find('i').toggleClass('icon-plus icon-minus');

关于javascript - 更改特定标签的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11900131/

相关文章:

javascript - 删除在图像 Canvas HTML5 上绘制的彩色线条

javascript - Cookie 仅在不使用 JSON.stringify 时设置

jquery - 插入符号仅在悬停在下拉导航栏上时显示

html - 创建一个向上打开的下拉菜单

javascript - 无法使用 JSON 中的 AngularJS 路径显示图像

javascript - react 和 Redux : 401 Unauthorized Error POST API Request

javascript - javascript验证中名称字段的正则表达式

javascript - 如何检查传递给 JavaScript 函数的值是否已定义或其长度是否 >=0?

jquery绝对位置动画

javascript - 如何在所有 jQuery 加载函数完成之前停止加载网页?