使用JavaScript将一个HTML表的内容复制到另一个HTML表

o4hqfura  于 2023-08-02  发布在  Java
关注(0)|答案(3)|浏览(161)

我正在自学JavaScript,我需要将一个表复制到另一个表中。我已经能够用这个例子复制@Gushiken的code和@Quentin的response
超文本标记语言:

<button onclick="copytable()">Copy Table</button>

<br />
<br />

<table id="TableA">
    <!--<tbody></tbody>-->
</table>
<br />

<table id="TableB" style="border:solid">
    <thead>
        <tr>
            <td id="col1">Column 1</td>
            <td id="col2">Column 2</td>
            <td id="col3">Column 3</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Data a</td>
            <td>Data b</td>
            <td>Data c</td>
        </tr>
    </tbody>
</table>
<br />

字符串
JavaScript:

function copytable() {
  var source = document.getElementById('TableB');
  var destination = document.getElementById('TableA');
  var copy = source.cloneNode(true);
  copy.setAttribute('id', 'tableB');
  destination.parentNode.replaceChild(copy, destination);
}


但是,一旦复制了表B,我如何访问复制表中的元素?即,表B是“硬编码”HTML,而…表A在内存中吗?我想把第一栏改成A栏,第二栏改成B栏,以此类推在表复制事件之后。
另外,如何删除复制的表?

arknldoa

arknldoa1#

要删除旧的TableB,可以尝试source.outerHTML = "";
然后删除对象:delete(source);
关于在tableA中插入TableB,我不明白。您想像<tableA><tableB></tableB></tableA>一样插入,还是只移动TableB的<td>

7xllpg7q

7xllpg7q2#

因为您已经为副本应用了一个新的id,所以可以使用它来访问克隆的元素。

function copytable() {
  var source = document.getElementById('TableB');
  var destination = document.getElementById('TableA');
  var copy = source.cloneNode(true);
  copy.setAttribute('id', 'tableA');
  destination.parentNode.replaceChild(copy, destination);
}

function deletCopy() {
  var el = document.getElementById('tableA');
  el.innerHTML = '';
}

个字符
但是访问新表中的元素的问题是,由于克隆的内部元素有id,所以有多个元素具有相同的id。如果可以使用class而不是id作为td元素,则

function copytable() {
  var source = document.getElementById('TableB');
  var destination = document.getElementById('TableA');
  var copy = source.cloneNode(true);
  copy.setAttribute('id', 'TableA');
  destination.parentNode.replaceChild(copy, destination);
}

function deletCopy() {
  var el = document.getElementById('TableA');
  el.innerHTML = '';
}

function changeCopy(){
  var el = document.getElementById('TableA');
  el.querySelector('.col1').innerHTML = 'Column A';
  el.querySelector('.col2').innerHTML = 'Column B';
  el.querySelector('.col3').innerHTML = 'Column C';
}
<button onclick="copytable()">Copy Table</button>
<button onclick="deletCopy()">Delete Table</button>
<button onclick="changeCopy()">Update Table</button>

<br />
<br />

<table id="TableA">
  <!--<tbody></tbody>-->
</table>
<br />

<table id="TableB" style="border:solid">
  <thead>
    <tr>
      <td class="col1">Column 1</td>
      <td class="col2">Column 2</td>
      <td class="col3">Column 3</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data a</td>
      <td>Data b</td>
      <td>Data c</td>
    </tr>
  </tbody>
</table>
<br />

的字符串

qij5mzcb

qij5mzcb3#

要删除源表,可以执行以下操作:

var tableB = document.getElementById('TableB');
tableB.parentNode.removeChild(tableB);

字符串
您可以在复制后立即更改复制的表,例如:

function copytable() {
  var source = document.getElementById('TableB');
  var destination = document.getElementById('TableA');
  var copy = source.cloneNode(true);
  copy.id = 'TableA';

  /* you can change what you want already here, e.g: */
  [].forEach.call(copy.querySelectorAll('td'), function(item, index) {
    switch (item.textContent.trim()) {
      case 'Column 1':
        item.textContent = 'Column A';
        break;
      case 'Column 2':
        item.textContent = 'Column B';
        break;
      case 'Column 3':
        item.textContent = 'Column C';
        break;
    }
  });

  destination.parentNode.replaceChild(copy, destination);
}

function deleteTableB() {
  var tableB = document.getElementById('TableB');
  tableB.parentNode.removeChild(tableB);
}
<button onclick="copytable()">Copy Table</button>
<button onclick="deleteTableB()">Delete TableB</button>

<br />
<br />

<table id="TableA">
  
</table>
<br />

<table id="TableB" style="border:solid">
  <thead>
    <tr>
      <td id="col1">Column 1</td>
      <td id="col2">Column 2</td>
      <td id="col3">Column 3</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data a</td>
      <td>Data b</td>
      <td>Data c</td>
    </tr>
  </tbody>
</table>
<br />

的数据

相关问题