jQuery 如何使用jQuery选择和更改表格单元格的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4737476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select and change value of table cell with jQuery?
提问by abenci
How do I change the content of the cell containing the 'c' letter in the HTML sample using jQuery?
如何使用 jQuery 更改 HTML 示例中包含“c”字母的单元格的内容?
<table id="table_header">
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
</table>
Thanks.
谢谢。
回答by hunter
$("td:contains('c')").html("new");
or, more precisely $("#table_headers td:contains('c')").html("new");
或者,更准确地说 $("#table_headers td:contains('c')").html("new");
and maybe for reuse you could create a function to call
也许为了重用,您可以创建一个函数来调用
function ReplaceCellContent(find, replace)
{
$("#table_headers td:contains('" + find + "')").html(replace);
}
回答by user113716
Using eq()
you can target the third cell in the table:
使用eq()
您可以定位表格中的第三个单元格:
$('#table_header td').eq(2).html('new content');
If you wanted to target everythird cell in each row, use the nth-child-selector
:
如果要定位每行中的每三个单元格,请使用nth-child-selector
:
$('#table_header td:nth-child(3)').html('new content');
回答by Igor
You can use CSS selectors.
您可以使用CSS 选择器。
Depending on how you get that td, you can either give it an id:
根据您获得该 td 的方式,您可以为其指定一个 id:
<td id='cell'>c</td>
and then use:
然后使用:
$("#cell").text("text");
Or traverse to the third cell of the first row of table_header, etc.
或者遍历到table_header第一行的第三个单元格,等等。
回答by Iman
i was looking for changing second row html and you can do cascading selector
我正在寻找更改第二行 html,您可以执行级联选择器
$('#tbox1 tr:nth-child(2) td').html(11111)
回答by PRTJ
I wanted to change the column value in a specific row. Thanks to above answers and after some serching able to come up with below,
我想更改特定行中的列值。多亏了上面的答案,经过一些搜索才能在下面提出,
var dataTable = $("#yourtableid");
var rowNumber = 0;
var columnNumber= 2;
dataTable[0].rows[rowNumber].cells[columnNumber].innerHTML = 'New Content';
回答by PutihTua
You can do this :
你可以这样做 :
<table id="table_header">
<tr>
<td contenteditable="true">a</td>
<td contenteditable="true">b</td>
<td contenteditable="true">c</td>
</tr>
</table>