首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何仅在表头上单击gettinmg时应用样式,单击其他表标头时移除样式?

问如何仅在表头上单击gettinmg时应用样式,单击其他表标头时移除样式?
EN

Stack Overflow用户
提问于 2016-07-20 07:12:18
回答 4查看 68关注 0票数 0

我有一张有7列的桌子。下面是HTML代码:

代码语言:javascript
复制
<table id="multiple-account-table" cellpadding="0" cellspacing="0">
    <thead>
        <tr class="border-class">
            <th>Employee Name</th>
            <th>Account Number</th>
            <th>Account Name</th>
            <th>Alias</th>
            <th>Due Date</th>
            <th>Total Due</th>
            <th>Payment Amount</th>
        </tr>
    </thead> 
</table>

现在我的要求是,当我单击表标题时,特定的边框应该在底部有粗边框。当用户单击其他表头时,该边框应从先前的表头中移除,并应用于当前单击的表头。如下所示:

点击这里查看图像

我编写了这个jQuery代码:

代码语言:javascript
复制
$(document).on('click', 'thead', function () {
    $(this).addClass('sort-border');
});

下面是排序边框的CSS类:

代码语言:javascript
复制
.sort-border {
    border-bottom: 4px solid black;
}

效果很好。但是,当我单击其他表标题时,先前单击的表标题的边框不会移除。有什么建议吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-07-20 07:16:43

如果您首先尝试删除任何标题上的类,这应该是类,那么sort-border类只有一个标头。

代码语言:javascript
复制
$(document).on('click', 'th', function () {
    $('th.sort-border').removeClass('sort-border');
    $(this).addClass('sort-border');
});

而且,thead会将边框给整条线,如果选择th,它将仅是边框的那个单元格。

票数 2
EN

Stack Overflow用户

发布于 2016-07-20 07:16:44

​

代码语言:javascript
复制
$(document).on('click', 'thead', function () {
  $('thead').removeClass('sort-border');
   $(this).addClass('sort-border');
});
代码语言:javascript
复制
.sort-border {
  background: #ccc;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="multiple-account-table" cellpadding="0" cellspacing="0">
  <thead>
    <tr class="border-class">
      <th>Employee Name</th>
      <th>Account Number</th>
      <th>Account Name</th>
      <th>Alias</th>
      <th>Due Date</th>
      <th>Total Due</th>
      <th>Payment Amount</th>
    </tr>
  </thead> 
</table>
<table id="multiple-account-table" cellpadding="0" cellspacing="0">
  <thead>
    <tr class="border-class">
      <th>Employee Name</th>
      <th>Account Number</th>
      <th>Account Name</th>
      <th>Alias</th>
      <th>Due Date</th>
      <th>Total Due</th>
      <th>Payment Amount</th>
    </tr>
  </thead> 
</table>

​

票数 1
EN

Stack Overflow用户

发布于 2016-07-20 07:19:35

做这样的事:

代码语言:javascript
复制
$(document).ready(function(){
  $('.border-class th').on('click'.function(){
     $('.border-class th').removeClass('sort-border');
     $(this).addClass('sort-border');
  });
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38474622

复制
相关文章

相似问题

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