我試圖在每次單擊添加按鈕時在表中創建一個按鈕。我在td中顯示的按鈕有問題。我確定它與我的JS有關。任何幫助,將不勝感激。 HTML:使用javascript在td中創建按鈕
<button class="btn btn-primary pull-right" onclick="myCreateFunction()">
Add new
</button>
<table class="table table-striped" border="4" cellpadding="3" cellspacing="1" id="usertable">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Title</th>
<th>Supervisor</th>
<th>Holiday</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php while ($rowemployeelist = mysql_fetch_array($employeelistresult)) { ?>
<tr>
<td>
<?php echo $rowemployeelist["first_name"] ?>
</td>
<td>
<?php echo $rowemployeelist ["last_name"] ?>
</td>
<td>
<?php echo $rowemployeelist ["title"]?>
</td>
<td>
<?php echo $rowemployeelist ["supervisor"]?>
</td>
<td>
<?php echo $rowemployeelist ["holiday"]?>
</td>
<td><button class="editbtn">Edit</button></td>
<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>
</tr>
<?php } ?>
</table>
JS:
function myCreateFunction() {
var table = document.getElementById("usertable");
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell5 = row.insertCell(5);
var cell6 = row.insertCell(6);
cell1.innerHTML = "";
cell2.innerHTML = "";
cell3.innerHTML = "";
cell4.innerHTML = "";
var button = document.createElement("button")
button. innerHTML = "Do Something";
var td = document. getElementsByTagName("td")[0];
td.appendChild(button);
}
正確,我希望它被添加到第五單元 –
@LucasBrawdy看我的編輯,你可以直接將子添加到第五單元。 –