简体   繁体   English

单击时突出显示表格单元格

[英]Highlighting table cells when clicked

I got this table.我得到了这张桌子。 What I would like to do is when I click a cell it should be highlighted, and with a second click the highlight should be cleared.我想要做的是当我点击一个单元格时它应该被突出显示,第二次点击应该清除突出显示。 and the second problem is I would like to highlight several cells one by one keeping the previous highlights on.第二个问题是我想一个一个突出显示几个单元格,保持以前的突出显示。 The fiddle is here: http://jsfiddle.net/2Lu3ss9g/小提琴在这里: http : //jsfiddle.net/2Lu3ss9g/

 $(function() { $('td').click(function() { $(this).parents('table').find('td').each(function(index, element) { $(element).removeClass('on'); }); $(this).addClass('on'); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <table class="color_changing" border="1" cellpadding="15"> <tbody> <tr> <td>23</td> <td>57</td> <td>62</td> <td>1162</td> </tr> <tr> <td>112</td> <td>5</td> <td>162</td> <td>88</td> </tr> <tr> <td>77</td> <td>62</td> <td>199</td> <td>211</td> </tr> <tr> <td>57</td> <td>64</td> <td>144</td> <td>9</td> </tr> </tbody> </table>

Here's a simple solution using toggleClass :这是一个使用toggleClass的简单解决方案:

$(function () {
    $('td').click(function () {
        $(this).toggleClass('highlight');
    });
});

Fiddle: http://jsfiddle.net/rqeec7r4/小提琴: http : //jsfiddle.net/rqeec7r4/

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

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