2015-06-12 14 views

回答

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

相關問題