![](/img/trans.png)
[英]Get contents of the first cell in the clicked column and the first cell of the clicked row in a HTML table
[英]get item from table cell and append to first cell within same row
如何從表格內的TD抓取文本並將其插入同一行的第一個TD? -使用jQuery
<table border=1>
<tbody>
<tr>
<td>APPEND TEXT1 HERE</td>
<td></td>
<td>GRAB TEXT1 HERE</td>
</tr>
<tr>
<td>APPEND TEXT2 HERE</td>
<td></td>
<td>GRAB TEXT2 HERE</td>
</tr>
<tr>
<td>APPEND TEXT3 HERE</td>
<td></td>
<td>GRAB TEXT3 HERE</td>
</tr>
</tbody>
</table>
$('table td:first-child').text(function(){
return $(this).siblings().last().text()
})
如果您要附加文本( 而不是替換 ):
$('table td:first-child').text(function(i, c){
return c + ' ' + $(this).siblings().last().text()
})
遍歷每一行,並將td
中的文本以所需的偏移量附加到第一行:
$('tr').each(function(){
$(this).find('td').eq(0).append($(this).find('td').eq(2).text());
});
這是一個演示: http : //jsfiddle.net/pchEG/
循環遍歷每個<tr>
並從最后一個<td>
子元素中獲取.html()
。
$('tr').each(function(idx){
var appendToNode = $(this).find('td:first-child');
// And append to the existing HTML.
appendToNode.html(appendToNode.html() + $(this).find('td:last-child').html());
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.