简体   繁体   English

如何使用javascript将隐藏的div标签的内容推到表行?

[英]How to push content of a hidden div tag to a table row using javascript?

I have a table with add more button which adds a certain number of rows with inputs in it. 我有一个带有添加更多按钮的表,它在其中添加了一定数量的带有输入的行。 But the HTML string for these rows have grown large enough and its becoming a pain. 但是这些行的HTML字符串已经足够大,这变得很痛苦。 So, what I am thinking is to have a div hidden tag with the required HTML that is repeated again and again on clicking the add more button. 因此,我在想的是让div隐藏标签具有所需的HTML,并在单击添加更多按钮时一次又一次地重复该标签。 So, how to push the div tag innerHTML inside a TR. 因此,如何将div标签推入TR内部。 Below is the code that I need to push, so I kept it inside the hidden DIV tag. 下面是我需要推送的代码,因此我将其保留在隐藏的DIV标签内。

<tr>
            <td><label for="bucket_size"><b>1.</b> Bucket Size:</label></td>
            <td><input type="text" name="bucket_size[]" id="bucket_size"></td>
            <td><label for="control_bucket_size">Control Bucket Size: </label></td>
            <td><input type="text" name="control_bucket_size[]" id="control_bucket_size" value="0"></td>
        </tr>
        <tr>
            <td><label for="from_date">Active From: </label></td>
            <td><input type="text" name="from_date[]" id="from_date" class="hasDatePicker"></td>
            <td><label for="to_date">To: </label></td>
            <td><input type="text" name="to_date[]" id="to_date" class="hasDatePicker"></td> 
        </tr>
        <tr>
            <td><label for="location">Location: </label></td>
            <td class="locationSelect">                   
            </td>
            <td style="text-align:center;" colspan="2"></td>
        </tr>

It doesn't get inserted properly if I directly insert after the particular TR, the innerHTML of the div tag. 如果我直接插入特定TR(即div标签的innerHTML)之后,则无法正确插入。 How to do it? 怎么做?

If you're trying to clone a table row then use another table row, not a div. 如果您要克隆一个表行,请使用另一个表行,而不是div。 And don't use innerHTML, use cloneNode 而且不要使用innerHTML,请使用cloneNode

<table>
    <tr id="clone_me" style="display:none">
         <td>...</td>
    </tr>
</table>

<script>
var original_node = document.getElementById('clone_me');
var clone_node = original_node.cloneNode(true);
clone_node.removeAttribute('id'); // Prevent duplicate ID
try {
   clone_node.style.display = 'table-row';
}
catch(iesucks) {
   clone_node.style.display = 'block'; // Should be 'table-row' but need this hack for IE 6-7
}
original_node.parentNode.appendChild(clone_node); // Add new node to end of table
</script>

If you find older IE's struggle with display:table-row then try toggling visibility instead. 如果发现较旧的IE在display:table-row遇到困难,请尝试切换visibility

You can't have a TR as a child of a DIV, even if the DIV is hidden (it's invalid markup). 即使DIV被隐藏(它是无效的标记),您也不能将TR作为DIV的子级。 You can't modify a table by changing its innerHTML in IE < 9 (though you can write an entire table or change the contents of a cell). 您无法通过在IE <9中更改表的innerHTML来修改表(尽管您可以编写整个表或更改单元格的内容)。 Use DOM methods. 使用DOM方法。

Likely the best method is to clone a row, then update name and id properties, plus anything else that needs it, and append it to the TBODY. 最好的方法可能是克隆一行,然后更新名称和id属性,以及需要它的其他所有内容,并将其附加到TBODY。 If you append to the TABLE, most browsers are OK but IE will barf, so append to the TBODY. 如果将其附加到TABLE,则大多数浏览器都可以,但是IE会禁止,因此将其附加到TBODY。

Extremely simple example: 极其简单的示例:

<table>
  <tr onclick="this.parentNode.appendChild(this.cloneNode(true))">
    <td>Here is a cell in a row
    <td>Here is a cell in a row
</table>

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

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