[英]get item from table cell and append to first cell within same row
How would I grab text from a td inside a table and insert it to the first td in the same row? 如何从表格内的TD抓取文本并将其插入同一行的第一个TD? --using jQuery
-使用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()
})
http://jsfiddle.net/VdjN8/ http://jsfiddle.net/VdjN8/
In case that you want to append the text( instead of replacing ): 如果您要附加文本( 而不是替换 ):
$('table td:first-child').text(function(i, c){
return c + ' ' + $(this).siblings().last().text()
})
Iterate over each row and append the text from the td
at the desired offset to the first one: 遍历每一行,并将
td
中的文本以所需的偏移量附加到第一行:
$('tr').each(function(){
$(this).find('td').eq(0).append($(this).find('td').eq(2).text());
});
Here is a demonstration: http://jsfiddle.net/pchEG/ 这是一个演示: http : //jsfiddle.net/pchEG/
Loop over each <tr>
and grab the .html()
from the last <td>
child element. 循环遍历每个
<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.