javascript - 在表数据上添加/删除类 onclick

标签 javascript css addclass removeclass

我想为表格单元格添加一个类,该类与悬停颜色 onclick 或单元格的选择相匹配。

我有这个 javascript(目前不工作):

$('#tblContainer').click(function() {
    var $this = $(this);

    // Remove highlight
    $this.closest("tr").find("td.guidelines").removeClass("guidelines");

    // Add it to this one
    $this.closest("td").addClass("guidelines2");
});

有了这 3 个主要类(指南、指南 2- 我希望它更改为的类,以及指南:悬停):

table.rubrictable td.guidelines {
    background: #FFF; 
    padding: 6px;
    text-align:left;
    color:#666666;
    font-size:9pt;
    font-style:plain;
    border-right: 1px solid #eeeeee;
    border-left: 1px solid #eeeeee;
    border-bottom: 2px solid #666666;
    width:150;
    background: #FFF; 
    background: -moz-linear-gradient(left top , #E6E6E6, #FFF); 
    background: -webkit-gradient(linear, left top, right bottom, color-stop(0%,#E6E6E6), color-stop(100%,#FFF));
}
table.rubrictable td.guidelines2 {
    background: #3C0; 
    padding: 6px;
    text-align:left;
    color:#666666;
    font-size:9pt;
    font-style:plain;
    border-right: 1px solid #eeeeee;
    border-left: 1px solid #eeeeee;
    border-bottom: 2px solid #666666;
    width:150;
}
table.rubrictable td.guidelines:hover {
    background:#3C0;
}

这是我的 html:

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tblContainer" class="rubrictable">
    <th class="dimensionstitle">ROWS (Dimensions)</th>
    <th class="level">Delicious<br />
      4</th>
    <th class="level">Good<br />
      3</th>
        <th class="level">Needs Improvement<br /
          2</th>
        <th class="level">Poor <br />
          1<a href="#" title="Remove this performance level."></a>
          </th>
        <th class="dimensionstitle">COMMENTS</th>
        </tr>
  <tr>
    <td class="dimensions" nowrap="nowrap">Number of Chips

      &nbsp;</td>
    <td class="guidelines">Chocolate chip in every bite</td>
    <td class="guidelines">Chips in about 75% of bites</td>
    <td class="guidelines">Chips in about 50% of bites</td>
    <td class="guidelines"Too few or too many chips</td>
    <td class="dimensions"><img src="Icons/edit-green.gif" width="16" height="16" /></td>
    </tr>
  <tr>
    <td class="dimensions">Texture&nbsp;</td>
    <td class="guidelines">Chew</td>
    <td class="guidelines">Chewy in middle, crisp on edges</td>
    <td class="guidelines">Texture either crispy/crunchy or 50% uncooked</td>
    <td class="guidelines">Texture resembles a dog biscuit</td>
    <td class="dimensions"><img src="Icons/edit-green.gif" width="16" height="16" /></td>
    </tr>
</table>

你可以在我的 FIDDLE 中看到一个例子

最佳答案

您已将 click() 处理程序放在表本身上,因此当您执行 $this.closest("tr") 时,它正在寻找一个元素那是表的祖先(不是子)并且是 tr。它不会找到它。

只需将点击声明更改为

$('#tblContainer td').click(function() {

关于javascript - 在表数据上添加/删除类 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15029825/

相关文章:

javascript - Firefox 中全屏 Bootstrap 轮播插件的问题

javascript - 请求完成后页面刷新

css - 在模板中添加 css 下拉菜单

javascript - 尝试这样做时警告框未关闭

jquery - 将类更改为标签,其中标签之间的类 ="xy"

Drupal 站点中的 jQuery addClass() 函数在 IE7 中不起作用

javascript - 获取ReactJS中每个父评论的最深评论

javascript - JavaScript 中的正则表达式未按预期工作

jquery - 固定位置 div 不会淡出

javascript - 如何找到 DIVS 的 ID,并将这些名称作为类分配给其他 DIVS?