简体   繁体   English

在html表的另一行中复制特定行

[英]Copy specific row in another row in html table

Hello I have difficulties to find a simple solution, maybe some of you will be able to help me. 您好,我很难找到简单的解决方案,也许你们中的一些人可以为我提供帮助。

What I would like to do is merge information from two differents columns in another row from the table. 我想做的是合并表另一行中两个不同列的信息。

Let me explain with an exemple it will be easier. 让我以一个例子来解释,这会更容易。

Here current html code : 这是当前的html代码:

<table class="audit_table" id="audit_table_data">

<tr>
  <th>Name</th>
  <th>Interfaces</th>
  <th>Subnet</th>
</tr>

<tr id="74">
  <td>water</td>
  <td>Ethernet</td>
  <td>Default</td>
</tr>

<tr id="74">
  <td>water</td>
  <td>Test</td>
  <td>Sub_Test</td>
</tr>

</table>

Here the expect result : 这是预期的结果:

<table class="table_class" id="table">
<tr>
  <th>Name</th>
  <th>Interfaces</th>
  <th>Subnet</th>
</tr>

<tr id="74">
  <td>water</td>
  <td>Ethernet<br>Test</td>
  <td>Default<br>Sub_Test</td>
</tr>
</table>

As the two tr elements have the same id, is this possible to do that ? 由于两个tr元素具有相同的ID,是否可以这样做?

your should use same class for two 'tr' elements: 您应该对两个“ tr”元素使用相同的类:

 <table class="audit_table" id="audit_table_data"> <tr> <th>Name</th> <th>Interfaces</th> <th>Subnet</th> </tr> <tr class="74"> <td rowspan="2">water</td> <td>Ethernet</td> <td rowspan="2">Default</td> </tr> <tr> <td>test</td> </tr> <tr class="74"> <td rowspan="2">water</td> <td>Test</td> <td rowspan="2">Sub_Test</td> </tr> <tr> <td>sub_test</td> </tr> </table> 

It is not correct to have the same ID twice in a page, but, as a workaround you can change one of them to something different and then call 'getElementById' again to return you another element. 在页面中两次具有相同的ID是不正确的,但是,作为一种解决方法,您可以将其中一个更改为不同的名称,然后再次调用'getElementById'以返回另一个元素。 Below is a sample of what you need actually: 以下是您实际需要的样本:

 //get the first element (row) by id var a = document.getElementById(74); //assign a different id to it a.setAttribute("id", "99"); //get the second element (row) now var b = document.getElementById(74); //get nr of columns var columns = a.getElementsByTagName('td').length; for (var i = 0; i < columns; i++) { //if we have the same string in the same column for both rows we do nothing if (a.getElementsByTagName('td')[i].innerHTML == b.getElementsByTagName('td')[i].innerHTML) continue; //we add the column from the second row to the first row a.getElementsByTagName('td')[i].innerHTML += "<br>" + b.getElementsByTagName('td')[i].innerHTML; } //we remove the second row remaining with the first one which was previously modified b.remove(); 
 <table class="audit_table" id="audit_table_data"> <tr> <th>Name</th> <th>Interfaces</th> <th>Subnet</th> </tr> <tr id="74"> <td>water</td> <td>Ethernet</td> <td>Default</td> </tr> <tr id="74"> <td>water</td> <td>Test</td> <td>Sub_Test</td> </tr> </table> 

What I did above was to modify the first row to contain in the cells both strings as information and then delete the second row. 我在上面所做的是修改第一行以将两个字符串都包含在单元格中作为信息,然后删除第二行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM