为了账号安全,请及时绑定邮箱和手机立即绑定

如何在纯JavaScript中将新值插入克隆的表行的单元格中?

如何在纯JavaScript中将新值插入克隆的表行的单元格中?

哔哔one 2022-09-02 21:00:50
 function copy_checklist(row_id, poNum) {            var row = document.getElementById("item"); //get table row            var table = document.getElementById("checklist");            var clone = row.cloneNode(true);            clone.id = row_id;            var tds = null;            for (var i = 0; i < clone.length; i++) {                tds = clone[i].getElementsByTagName("td");                for (var n = 0; n < tds.length; n++) {                    tds[n].innerHTML = poNum;                }            }            table.appendChild(clone);        }<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'><head>    <meta charset='utf-8'/>    <title></title>    <style>tr {height: 40px;}</style></head><body  onLoad="copy_checklist('1', 'po-202')"><table border='0' cellspacing='0' cellpadding='0' style='font-family: Arial; font-size: 12px;'>  <tbody id="checklist">        <tr id="item">            <td nowrap='nowrap'                style='padding:5px 2px;border:1px solid #000000;border-top:0px;font-family:Arial;font-size:12px;text-align:center!important;word-break: break-word;width:78px'>                <div id="poNum">po2</div>            </td>            <td nowrap='nowrap'                style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:77px'>                <div id="skuNum">sku</div>            </td>            <td nowrap='nowrap'                style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:85px'>                <div id="itemDesc">desc</div>            </td>我正在使用以下函数 -尝试设置新克隆的 tr 的 td 的新值,但它仅显示旧值。它可以很好地克隆 tr 元素,但仅显示旧值。我希望新克隆的tr的单元格将新值传递给函数。而且,如果我尝试通过执行findElementsByTag来检查这个新克隆的tr的长度,它将返回0。
查看完整描述

1 回答

?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

您可以通过使用然后迭代该集合或按索引访问特定单元格来简化对元素的访问<td>clone.cells


function copy_checklist(row_id, poNum) {

  var row = document.getElementById("item"); //get table row

  var table = document.getElementById("checklist");

  var clone = row.cloneNode(true);

  var cells = clone.cells;


  clone.id = row_id;

  cells[0].innerHTML = poNum

  // OR

  //Array.from(cells).forEach((cell, index) => cell.innerHTML = poNum);


  table.appendChild(clone);

}

<!DOCTYPE html>

<html xmlns='http://www.w3.org/1999/xhtml'>


<head>

  <meta charset='utf-8' />

  <title></title>

  <style>

    tr {

      height: 40px;

    }

  </style>

</head>


<body onLoad="copy_checklist('1', 'po-202')">

  <table border='0' cellspacing='0' cellpadding='0' style='font-family: Arial; font-size: 12px;'>

    <tbody id="checklist">

      <tr id="item">

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-top:0px;font-family:Arial;font-size:12px;text-align:center!important;word-break: break-word;width:78px'>

          <div id="poNum">po2</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:77px'>

          <div id="skuNum">sku</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:85px'>

          <div id="itemDesc">desc</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:58px'>

          <div id="lotId">WK 14</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:65px'>

          <div id="lotSize">1</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:50px'>

          <div id="sampleSize">2</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:50px'>

          <div id="criticalDefects">3</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:50px'>

          <div id="majorDefects">4</div>

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;word-break: break-word;width:50px'>

          <div id="minorDefects">5</div>

        </td>

        <td style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;width:350px;'></td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;width:40px;word-break: break-word;width:50px'></td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;width:40px;word-break: break-word;width:50px'>

          See Pic

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;width:40px;word-break: break-word;width:50px'>

          Ok

        </td>

        <td nowrap='nowrap' style='padding:5px 2px;border:1px solid #000000;border-left:0px;border-top:0px;font-family:Arial;font-size:12px;width:40px;word-break: break-word;width:50px'>

          %

        </td>

      </tr>

    </tbody>

  </table>


查看完整回答
反对 回复 2022-09-02
  • 1 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信