2015-10-30 255 views
-2

我有一個html,當然,它有一個table元素,而在tbody中,我有一個將刪除表的行的按鈕。刪除tbody上的tr

我已將一個onclick=""函數分配給按鈕。 (Function here)

如何在表(THEAD)的頭放在桌子上(TBODY)的身體,而不是隻刪除行的行?

FIDDLE HERE

+0

分享您的代碼 –

+0

請發表你的代碼 –

+1

的【什麼是消除與jQuery一表行的最好方法?(HTTP可能重複:// stackoverflow.com/questions/170997/what-is-the-best-way-to-remove-a-table-row-with-jquery) –

回答

0

裹體行tbody元,比你可以選擇只table tbody tr被刪除。使用jQuery你可以做$('table tbody tr').remove();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="table"> 
 
    <thead> 
 
    <tr> 
 
     <th>Name</th> 
 
     <th>Action</th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td>Some content</td> 
 
     <td> 
 
     <button onClick="$(this).closest('tr').remove()">Remove</button> 
 
     </td> 
 
    </tr> 
 

 
    <tr> 
 
     <td>Some content</td> 
 
     <td> 
 
     <button onClick="$(this).closest('tr').remove()">Remove</button> 
 
     </td> 
 
    </tr> 
 

 
    <tr> 
 
     <td>Some content</td> 
 
     <td> 
 
     <button onClick="$(this).closest('tr').remove()">Remove</button> 
 
     </td> 
 
    </tr> 
 

 
    <tr> 
 
     <td>Some content</td> 
 
     <td> 
 
     <button onClick="$(this).closest('tr').remove()">Remove</button> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<button onClick="$('#table tbody tr').remove()">Remove all rows</button>