简体   繁体   English

在没有jquery的索引处插入HTML表行克隆

[英]Insert HTML table row clone at index without jquery

I am attempting to insert a row at the index of the addmorebutton td row. 我正在尝试在addmorebutton td行的索引处插入一行。

First attempt indeed adds a row at the desired index however not as I would have expected, simply as space vs actually adding a bounded row column box. 第一次尝试确实确实在期望的索引处添加了一行,但是却没有达到我的预期,就像空间与实际添加有界的行列框一样。

How do I insert a new row at the clicked index of td that is empty and is indeed apart of the table? 如何在td的单击索引处插入一个新行,该行为空并且确实在表中?

 function deleteRow(row) { var i = row.parentNode.parentNode.rowIndex; document.getElementById('Table').deleteRow(i); } function addRow() { var i = row.parentNode.parentNode.rowIndex; document.getElementByID('Table').insertRow(i); } turkSetAssignmentID(); 
 <script type='text/javascript' src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js'></script> <form name='mturk_form' method='post' id='mturk_form' action='https://www.mturk.com/mturk/externalSubmit'> <input type='hidden' value='' name='assignmentId' id='assignmentId' /> <h1>This is a test</h1> <div id="tablediv"> <input type="button" id="addbutton" value="Add Index" /><br/><br/> <table id="Table" border="1"> <tr> <td>Index</td> <td>Measured Depth</td> <td>Inclination</td> <td>Azimuth</td> <td>Delete?</td> <td>Add Row?</td> </tr> <tr> <td>1</td> <td><input size=25 type="text" id="Measured Depth" contenteditable='true'></td> <td><input size=25 type="text" id="Inclination" contenteditable='true'></td> <td><input size=25 type="text" id="Azimuth" contenteditable='true'></td> <td><input type="button" id="delbutton" value="Delete" onclick="deleteRow(this)" /></td> <td><input type="button" id="addmorebutton" value="Add More Indexs" onclick="addRow.apply(this)" /></td> </tr> </table> </div> <p><input type='submit' id='submitButton' value='Submit' /></p> </form> 

You could try to add the respective cells to new row, saving the row on a var and the with the appendChild() function. 您可以尝试将相应的单元格添加到新行,将该行保存在var并使用appendChild()函数。 Something like this: 像这样:

 function addRow(row) { var i = row.parentNode.parentNode.rowIndex + 1; var nextRow = document.getElementById('Table').insertRow(i); // Start iterating over all the cells on the row var cell = nextRow.insertCell(0); cell.appendChild(document.createTextNode(i)); cell = nextRow.insertCell(); cell = nextRow.insertCell(); cell = nextRow.insertCell(); cell = nextRow.insertCell(); cell = nextRow.insertCell(); input = document.createElement("Input"); input.type = "button"; input.setAttribute("onclick", "addRow(this)"); input.value = "Add More Indexs" cell.appendChild(input); } 

And of course append on each cell the respective input. 当然,在每个单元格上附加各自的输入。 Then maybe you would like to add a function to increase the index number of the rows under the new one... 然后,也许您想添加一个函数来增加新索引下的行的索引数...

Also I changed the onclick value on the original button to addRow(this) . 另外,我将原始按钮上的onclick值更改为addRow(this)

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

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