简体   繁体   English

如何使用jQuery指定选定的contenteditable标签?

[英]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. 选择一个单元格意味着在该单元格中使用鼠标光标。

  1. How can I specify which cell is selected by user ? 如何指定用户选择的单元格? (mouse cursor is inside of it ) (鼠标光标在其中)
  2. How can I specify which cell is hovered by user ? 如何指定用户将鼠标悬停在哪个单元格上? (mouse cursor on it ) (将鼠标光标放在上面)

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. 希望这可以帮助。


Snippet Example 片段示例

 $('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 and blur events 使用focusblur事件

 $('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.

相关问题 如何在 html contenteditable div 标签中获取选定的文本位置? - How to get the selected text position in a html contenteditable div tag? 如何使用jQuery在contenteditable div中移动carret? - How to move the carret in a contenteditable div using jQuery? 如何使用JQuery使子元素的内容可编辑 - How to make contenteditable to child element using JQuery 如何使用jquery或javascript将锚标记添加到段落中的选定文本 - How to add a anchor tag to selected text in the paragraph using jquery or javascript JQUERY:如何使用文本框作为工具提示标记选定的文本 - JQUERY: How to tag selected text using textbox as tooltip 如何使用jquery查找多选标签的最新选定项? - how to find the latest selected item of a multiple select tag using jquery? 在PHP中使用jQuery Ajax在第一个contenteditable div超过高度后如何添加新的contenteditable div? - How to add new contenteditable div after first contenteditable div exceed the height using jquery ajax in php? 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 在contentEditable div中粗体选择文本,使用没有jQuery等库的纯JavaScript - Bold Selected Text in contentEditable div, using pure JavaScript without libraries such as jQuery 使用jquery保存可信的数据 - Saving contenteditable data using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM