我有一個按鈕以及一個表格行。當我點擊按鈕時,應該在表格中添加新行,並且按鈕應該存在於新添加的行中.refer圖片Java Script:在表格中動態添加文本字段和按鈕
-2
A
回答
0
下面是一個快速解決方案,可以添加帶有按鈕的新行並添加新行。
你沒有添加任何代碼,但這個工程。
https://jsfiddle.net/scheda/Lhsvmqoy/
var b = document.querySelector('.clicky')
var table = document.querySelector('table');
var insert_this = '<tr><td><input type="text" placeholder="Look ma!"/><button class="clicky">Add more stuff</button></td></tr>';
document.querySelector('body').addEventListener('click', function(e) {
if (e.target.className === 'clicky') {
table.innerHTML += insert_this;
}
});
0
像您期望這應該工作:
<!DOCTYPE html>
<head>
<style>
td,table{border:solid 1px;}
</style>
<title>Table sample </title>
</head>
<body>
<table id="myTable">
<tr>
<td>Row 1</td><td></td>
</tr>
<tr>
<td>Row 2</td><td><button id="newRow">New Row (original button)</button></td>
</tr>
</table>
</body>
<script>
function addRow() {
// Get a reference to the table
var tableRef = document.getElementById('myTable');
// Insert a row in the table at the end
var newRow = tableRef.insertRow(tableRef.rows.length);
// Insert a cell in the row at index 0
var newCell = newRow.insertCell(0);
newCell.innerHTML="Row " + tableRef.rows.length;
var newCell = newRow.insertCell(1);
// Append button node to the cell
var newButton = document.getElementById('newRow');
newCell.appendChild(newButton);
}
function addEvent(elem, event, fn) {
if (elem.addEventListener) {
elem.addEventListener(event, fn, false);
}else {
elem.attachEvent("on" + event, function() {
// set the this pointer same as addEventListener when fn is called
return(fn.call(elem, window.event));
});
}
}
var mybutton = document.getElementById("newRow");
addEvent(mybutton,"click",addRow);
</script>
</html>
來源/學分: 使用addEventListener功能:adding event listener cross browser
添加行功能(改) : https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement/insertRow
相關問題
- 1. 中添加動態文本字段添加到HTML表格
- 2. 在表格視圖上添加文本字段按鈕點擊
- 3. 在活動中添加動態輸入文本和按鈕android
- 4. 使用添加按鈕在文本字段中添加數字java
- 5. JAVA-無法將文本字段和按鈕添加到容器
- 6. 添加按鈕,動態也添加文本框,動態AngularJS
- 7. 按下按鈕後添加一個動態的表單字段
- 8. as3 - 在動態文本字段中逗號後添加空格
- 9. jQuery的動態添加表格字段,刪除表格字段
- 10. Ionic 2動態添加表格字段
- 11. AngularJS - 動態添加和刪除表格內的表格字段
- 12. 訪問一個動態文本字段和按鈕在ActionScript 3
- 13. 如何在文本字段中添加按鈕?
- 14. ckeditor如何使用對話框中的按鈕動態添加文本字段
- 15. 在as3中隨機添加文本到動態文本字段
- 16. JavaFX動態添加新文本字段
- 17. 如何渲染表格中的文本字段和按鈕?
- 18. 如何在Android中動態添加表格中的字段?
- 19. 在UITextView中添加按鈕和文本
- 20. 在WPF中動態添加按鈕到表格排列
- 21. 在網格中動態添加按鈕和Silverlight播放器?
- 22. JavaFX動態添加按鈕
- 23. 在as3中更改按鈕內的動態文本字段
- 24. 如何在文本輸入字段中添加「搜索」按鈕?
- 25. 添加一個按鈕,網格動態
- 26. 通過點擊添加按鈕動態添加表單輸入字段
- 27. 動態添加表單中的文本字段
- 28. 輸入文本字段和按鈕的寬度變化動態
- 29. JPanel不添加按鈕和文本字段
- 30. 如何以編程方式添加文本字段和按鈕?
你有任何的代碼?你有什麼嘗試? – Soma
請先創建一個樣本。 – nikhil
http://viralpatel.net/blogs/dynamically-add-remove-rows-in-html-table-using-javascript/ – vrajesh