本文介绍了如何使用javascript选择动态创建的表的表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 HI,对不起我的英语 我用c#创建了一个动态表,现在我想根据id和表格单元格选择表格单元格使用javascript点击它们时改变颜色。我怎么能实现这个? 谢谢。 我尝试了什么: 这是我的代码背后: private void CreateDynamicTable() { int rowCnt; int cellCnt; int rowCtr; int cellCtr; 表Table1 = new Table(); Table1.BorderWidth = 3; Table1.BorderStyle = BorderStyle.Solid; Table1.ID =myTable; rowCnt = int.Parse(txtrow.Text); cellCnt = int.Parse(txtcol.Text); for(rowCtr = 1; rowCtr< = rowCnt; rowCtr ++) { TableRow tRow = new TableRow(); Table1.Rows.Add(tRow); for(cellCtr = 1; cellCtr< = cellCnt; cellCtr ++) { TableCell tCell = new TableCell(); tRow.Cells.Add(tCell); string prodID = rowCtr +_+ cellCtr; TextBox txtbox = new TextBox(); txtbox.Text =Row:+ rowCtr +Column:++ cellCtr; tCell.Controls.Add(txtbox); tRow.Controls.Add(tCell); } Table1.Rows.Add(tRow); } form1.Controls.Add(Table1); } HTML: < html xmlns = http://www.w3.org/1999/xhtml > < head runat = 服务器 > < link href = StyleSheet1.css rel = 样式表 / > < title > < / title > < / head > < body > < 表单 id = form1 runat = 服务器 > < div > < 表 > 创建动态表 < tr > < td > 行: < asp:TextBox ID = txt row 占位符 = 此处没有行 runat = server AutoCompleteType = 已禁用 > < / asp:TextBox > < br / > < br / > < / td > < / tr > < tr > < td > Coloum: < asp:TextBox ID = txtcol 占位符 = 此处没有Coloums runat = server AutoCompleteType = 已禁用 > < ; / asp:TextBox > < br / > < br / > < / td > < / tr > < tr > < ; td > < asp:按钮 ID = Button1 文本 = 创建表 runat = server CssClass = button1 OnClick = OnClickOfCreateTable / > < / td > < / tr > & / table > < / div > < / body > < / html > 解决方案 您应该可以使用jQuery轻松完成。首先,定义一个css: 。 highlight { background-color : green; } 然后,您可以这样做: ( #myTable tr)。点击( function (){ var selected = ( this )。hasClass( highlight); HI, Sorry for my EnglishI have created a dynamic table using c#, Now I want to select the table cells based on id and that table cells to change color when they are clicked using javascript. How can i achieve this??Thanks.What I have tried:Here is my code behind:private void CreateDynamicTable(){ int rowCnt; int cellCnt; int rowCtr; int cellCtr; Table Table1 = new Table(); Table1.BorderWidth = 3; Table1.BorderStyle = BorderStyle.Solid; Table1.ID = "myTable"; rowCnt = int.Parse(txtrow.Text); cellCnt = int.Parse(txtcol.Text); for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++) { TableRow tRow = new TableRow(); Table1.Rows.Add(tRow); for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++) { TableCell tCell = new TableCell(); tRow.Cells.Add(tCell); string prodID = rowCtr + "_" + cellCtr; TextBox txtbox = new TextBox(); txtbox.Text = "Row:" + rowCtr + " Column:" + " " + cellCtr; tCell.Controls.Add(txtbox); tRow.Controls.Add(tCell); } Table1.Rows.Add(tRow); } form1.Controls.Add(Table1);}HTML:<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <link href="StyleSheet1.css" rel="stylesheet" /> <title></title></head><body> <form id="form1" runat="server"> <div> <table> Creating a dyanamic Table <tr> <td>Row: <asp:TextBox ID="txtrow" placeholder="No of Rows Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox> <br /> <br /> </td> </tr> <tr> <td>Coloum: <asp:TextBox ID="txtcol" placeholder="No of Coloums Here" runat="server" AutoCompleteType="Disabled"></asp:TextBox> <br /> <br /> </td> </tr> <tr> <td> <asp:Button ID="Button1" Text="Create Table" runat="server" CssClass="button1" OnClick="OnClickOfCreateTable" /> </td> </tr> </table> </div> </body> </html> 解决方案 You should be able to easily do that with jQuery. First, define a css:.highlight { background-color: green; }then, you can do something like this:("#myTable tr").click(function() { var selected =(this).hasClass("highlight"); 这篇关于如何使用javascript选择动态创建的表的表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 13:53