简体   繁体   English

根据按钮选择更改表格单元格颜色

[英]Change table cell color based on a button selection

I am trying to create a set of buttons that change the color of a clicked cell in a table. 我试图创建一组按钮来更改表中单击的单元格的颜色。

for example: 例如:

  1. User chooses the blue button 用户选择蓝色按钮

  2. Only one button is allowed to be active at a time. 一次只能激活一个按钮。

  3. Any cell that the user clicks on changes blue (or red, green, yellow or white depending on the button selected. 用户单击的任何单元格都会更改为蓝色(或红色,绿色,黄色或白色,具体取决于所选按钮)。

The best I could do is the picture below. 我能做的最好的是下面的图片。 Please help or guide me to a solution. 请帮助或指导我解决。 Thanks. 谢谢。


表格图片

Here is a working sample: 这是一个工作示例:

 var color = "White" $( "#btnBlue" ).click(function() { color = "Blue" }); $( "#btnGreen" ).click(function() { color = "Green" }); $( "#btnYellow" ).click(function() { color = "Yellow" }); $("table tr td").click(function(){ $(this).css("background-color", color); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> <body> <input id='btnBlue' type='button' value='Blue' > <input id='btnGreen' type='button' value='Green' > <input id='btnYellow' type='button' value='Yellow' > <br> <br> <table> <tr> <td>col1</td> <td>col2</td> </tr> <tr> <td>col3</td> <td>col4</td> </tr> </table> </body> 

https://jsfiddle.net/wx38rz5L/1242/ https://jsfiddle.net/wx38rz5L/1242/

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

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