用javascript克隆整个html表(带唯一ID)元素

时间:2013-12-05 21:55:38

标签: javascript html

我以某种方式使我的newRow()函数工作。您可以在下面看到代码。 现在我需要克隆整个表。 我试图调整newRow()函数来完成这项任务,显然我的尝试是不成功的。

我的js代码:

function newRow( valoda, dataTable){
    var row, table, len, tags, newRow;
    row = document.getElementById('valoda');
    table = document.getElementById('dataTable');
    tags = table.getElementsByTagName('tr');
    len = tags.length;

    newRow = row.cloneNode(true);
    newRow.id = "valoda_0" +  len;
    table.appendChild(newRow);
}

function newTable(dataTable3){
    var table, len, tags, newTable;

    table = document.getElementById('dataTable3');
    tags = table.getElementsByTagName('table');
    len = tags.length;

    newTable = row.cloneNode(true);
    newTable.id = "darbs_0" +  len;
    table.appendChild(newRow);
}

和HTML:

<fieldset class="row2">
   <legend>Iemaņas un zināšanas</legend>
   <p> 
      <input type="button" value="Pievienot" onClick="newRow( 'valoda', 'dataTable')" /> 
   <p>
   </p>
   </p>
   <table  class="virsraksti" border="1">
      <tr>
         <th style="width:25%"> Valoda</th>
         <th style="width:25%"> Runātprasme </th>
         <th style="width:25%"> Lasītprasme </th>
         <th style="width:25%"> Rakstītprasme </th>
      </tr>
   </table>
   <table id="dataTable" class="cv" border="1">
      <tbody>
         <tr id="valoda">
            <td>
               <input type="text" style="width:100%;" placeholder="ievadiet valodu">
            </td>
            <td>
               <select id="Runatprasme_izv" name="Runatprasme_izv" required="required">
                  <option>dzimtā valoda</option>
                  <option>teicami</option>
                  <option>labi</option>
                  <option>viduvēji</option>
                  <option>pamatzināšanas</option>
               </select>
            </td>
            <td>
               <select id="Lasitprasme_izv" name="Lasitprasme_izv" required="required">
                  <option>teicami</option>
                  <option>labi</option>
                  <option>viduvēji</option>
                  <option>pamatzināšanas</option>
               </select>
            </td>
            <td>
               <select id="Rakstitprasme_izv" name="Rakstitprasme_izv" required="required">
                  <option>teicami</option>
                  <option>labi</option>
                  <option>viduvēji</option>
                  <option>pamatzināšanas</option>
               </select>
            </td>
         </tr>
      </tbody>
   </table>
   <div class="clear"></div>
</fieldset>
<fieldset class="row3">
   <legend>Datorzināšanas</legend>
   <p>
      <input type="button" value="Pievienot" onClick="newRow2( 'datori', 'dataTable2')"/> 
   </p>
   <p>
   <table>
      <th>
      <th style="width:75%"> Nosaukums </th>
      <th>
      <th style="width:25%"> Zināšanu līmenis </th>
      </tr>
   </table>
   <table id="dataTable2" class="cv" border="1">
      <tr id="datori">
         <td style="width:75%"><input type="text" style="width:100%;" name="Dator_nos"placeholder="Ievadiet programmatūras nosaukumu"></td>
         <td>
            <select id="BX_gender" name="BX_gender" style="width:100%">
               <option>teicami</option>
               <option>labi</option>
               <option>pamatzināšanas</option>
            </select>
         </td>
      </tr>
   </table>
   </p>
</fieldset>
<fieldset class="row4">
   <legend>Darba pieredze</legend>
   <p> 
      <input type="button" value="Pievienot" onClick="newTable('dataTable3')" /> 
   <p></p>
   </p>
   <table id="dataTable3" class="cv" border="1">
      <tr>
         <td style="width:5%; text-align:left"> Darba vieta/Uzņēmums: </td>
         <td style="padding:0"> <input type="text" class="darbs"></td>
      </tr>
      <tr>
         <td style="width:5%; text-align:left"> Datums no: </td>
         <td style="padding:0"> <input type="text" class="darbs"></td>
      </tr>
      <tr>
         <td style="width:5%; text-align:left"> Datums līdz:</td>
         <td style="padding:0"> <input type="text" class="darbs"></td>
      </tr>
      <tr>
         <td style="width:5%; text-align:left"> Ieņemamais amats:</td>
         <td style="padding:0"> <input type="text" class="darbs"></td>
      </tr>
      <tr>
         <td style="width:5%; text-align:left"> Amata kategorija:</td>
         <td style="padding:0"> <input type="text" class="darbs"></td>
      </tr>
   </table>
</fieldset>

我知道克隆表行有很多方法,但由于我要将所有输入数据放入DB,我需要一个为每个创建的元素创建uniq ID的函数,我知道newRow( )函数做到了,但是addTable()呢?它是否只创建唯一的表ID,还是可以为表中的所有输入字段获取新的唯一ID?

我希望你明白我扭曲的问题。 根据要求,如果需要,我会添加一个小提琴。

你能告诉我正确的解决方案吗?

提前非常感谢。

2 个答案:

答案 0 :(得分:0)

https://developer.mozilla.org/en-US/docs/Web/API/Node.cloneNode


HTML代码 -

<table id="mytable">
    <thead>
        <tr>
            <th colspan=3>Values (Type A)</th>
        </tr>
        <tr>
            <th colspan=3>Values (Type B)</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td><td>2</td><td>3</td>
        </tr>
        <tr>
            <td>4</td><td>5</td><td>6</td>
        </tr>
    </tbody>
</table>

JavaScript代码 -

var mytable = document.querySelector('#mytable');
var clonedMyTable = mytable.cloneNode();
console.log(clonedMyTable);

http://jsfiddle.net/CLvD2/2/

答案 1 :(得分:0)

javascript:

$("#mytable").clone().appendTo(".mytarget");

HTML:

<table id="mytable">
<tr>
<td>cell1</td>
<td>cell2</td>
</tr>
</table>

<div class="mytarget"></div>