如何使用jquery将表中最后两个单元格的值替换为字符串

How to replace last two cells values in a table with a string using jquery?

本文关键字:单元格 两个 字符串 替换 jquery 何使用 最后      更新时间:2023-09-26
  <table>
     <tr>
      <td>aaaa</td>
      <td>bbbb</td>
      <td>cccc</td>
      <td>dddd</td>
     </tr>
  </table>

在上表中,我如何用字符串"abcd">

替换最后两个单元格值

您可以通过向slice((传递负起始索引来同时更改两个单元格的文本:

$("td").slice(-2).text("abcd");

以下内容将替换最后两个单元格中的值,但我不确定这是否是您真正想要的:

var cells = document.getElementsByTagName('td');
var i = cells.length;
cells[--i].innerHTML = 'abcd';
cells[--i].innerHTML = 'abcd';
  $('td').eq(-2).html('this is the second to last')
  $('td').eq(-1).html('this is the last')

这应该有效(你也可以使用:last来获得最后一个(。