[英]How to specifiy a selected contenteditable tag using jQuery?
I am writing an online Sudoku game and I want to change the background color of each cell , when the user select a cell. 我正在写在线数独游戏,当用户选择一个单元格时,我想更改每个单元格的背景颜色。 For example when I select a cell that contains "4", each cell with "4" value in it must become red, and after changing selected cell, background of each cell with "4" value changes to the default background(here is white).
例如,当我选择一个包含“ 4”的单元格时,其中每个具有“ 4”值的单元格必须变为红色,并且在更改所选单元格后,每个具有“ 4”值的单元格的背景将更改为默认背景(此处为白色)。 enter image description here
在此处输入图片说明
Note that each <td> </td>
represents a cell. 请注意 ,每个
<td> </td>
代表一个单元格。 and selecting a cell means mouse cursor in that cell. 选择一个单元格意味着在该单元格中使用鼠标光标。
More info : 更多信息 :
The HTML summary of Sudoku table is like this : Sudoku表的HTML摘要如下所示:
<table>
<tr>
<td contenteditable="true"></td>
<td contenteditable="false">2</td>
<td contenteditable="false">3</td>
</tr>
<tr>
<td contenteditable="false">4</td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
</tr>
<table>
You could use click()
event to detect selected cell and input
event to detect user change inside the input, then use each()
to go through all cells and colorize the matched ones, check the example below. 您可以使用
click()
事件检测选定的单元格,并使用input
事件检测输入内部的用户更改,然后使用each()
遍历所有单元格并为匹配的单元格上色,请查看下面的示例。
Hope this helps. 希望这可以帮助。
$('table').on('click input','td',function(){ var clicked_td_text = $(this).text(); $('td').each(function(){ if( clicked_td_text!='' && $(this).text()==clicked_td_text ) $(this).addClass('red'); else $(this).removeClass('red'); }) })
.red{ background-color: red !important; } table td{ width: 15px; text-align: center; } table td[contenteditable="false"]{ background-color: #E8F7E6; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table border=1> <tr> <td contenteditable="true"></td> <td contenteditable="false">2</td> <td contenteditable="false">3</td> <td contenteditable="false">5</td> <td contenteditable="true"></td> <td contenteditable="false">7</td> </tr> <tr> <td contenteditable="false">1</td> <td contenteditable="true"></td> <td contenteditable="false">5</td> <td contenteditable="false">2</td> <td contenteditable="true"></td> <td contenteditable="false">3</td> </tr> <tr> <td contenteditable="false">4</td> <td contenteditable="true"></td> <td contenteditable="false">6</td> <td contenteditable="true"></td> <td contenteditable="false">7</td> <td contenteditable="true"></td> </tr> <tr> <td contenteditable="false">4</td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="false">1</td> <td contenteditable="true"></td> <td contenteditable="false">5</td> </tr> <tr> <td contenteditable="true"></td> <td contenteditable="false">2</td> <td contenteditable="false">1</td> <td contenteditable="false">3</td> <td contenteditable="true"></td> <td contenteditable="false">8</td> </tr> <tr> <td contenteditable="false">5</td> <td contenteditable="true"></td> <td contenteditable="false">7</td> <td contenteditable="false">4</td> <td contenteditable="true"></td> <td contenteditable="false">6</td> </tr> <table>
Use
focus
andblur
events使用
focus
和blur
事件
$('table').on('focus input', 'td[contenteditable]', function() { $('td[contenteditable]').css('background-color', ''); var val = this.textContent; if (val) { $('td[contenteditable]:contains(' + val + ')').css('background-color', 'red'); } }); $('table').on('blur', '[contenteditable]', function() { $('td[contenteditable]').css('background-color', ''); });
table, td { border: 2px solid black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <table> <tr> <td contenteditable="true"></td> <td contenteditable="false">2</td> <td contenteditable="false">3</td> </tr> <tr> <td contenteditable="false">4</td> <td contenteditable="true"></td> <td contenteditable="true"></td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.