简体   繁体   English

从表单元格获取项目并追加到同一行中的第一个单元格

[英]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());
});

http://jsfiddle.net/Lvfwt/ http://jsfiddle.net/Lvfwt/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取单击列中第一个单元格的内容以及HTML表格中单击行的第一个单元格 - Get contents of the first cell in the clicked column and the first cell of the clicked row in a HTML table 如何在js jquery中将单元格附加到表行 - how to append a cell to a table row in js jquery 使用jquery获取表格第一行每列单元格的宽度 - Get width of each column cell of first row of a table using jquery JavaScript-获取选定表行的第一个单元格的数据 - JavaScript - Get data of first cell of selected table row 有没有办法只使用javascript从html表格行中的第一个单元格获取值? - is there a way to get the value from the first cell in html table row using javascript only? 如何从html表中选择行并获取第一个单元格的值并将其放在PHP Session上 - How select row from html table and get the value of the first cell and put it on PHP Session 如何从单元格内的锚访问表行对象? - how to access table row object from an anchor within a cell? 将文本附加到同一单元格后,表格单元格中的 Indesign 字形消失了 - Indesign Glyphs in table cell disappeared after append text to the same cell 如何从用户悬停的行中获取第一个单元格? - How to get a first cell from a row that user hovers over? 单击第一个单元格以外的表格行 - Click on table row except first cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM