Jquery method to remove tr: 1. Create a js sample file; 2. Introduce jQuery; 3. Use the "function removeTr(temp){...}" method to remove tr.
The operating environment of this article: windows7 system, jquery-2.1.4 version, DELL G3 computer
How to remove tr from jquery?
jQuery remove tr
When I was working on a project today, I encountered a problem, which was to remove some tr (tr was added dynamically) . I have tried many methods, but none of them work (for example, in the deleteRow method, it seems that the parameter passed can only be the number of rows of tr. I have not studied it carefully so far). Later, I found that this method worked well, so I will record it here.
$(temp).parent().remove(); //temp为td的id。
My understanding is this: $(temp) first obtains the td object, .parent() obtains the tr of the td, and then remove() method to delete the tr
html code:
<table> <tr> <td><a href='#' onclick='removeTr(this)'>123</a></td> <td><a href='#' onclick='removeTr(this)'>456</a></td> </tr> <tr> <td><a href='#' onclick='removeTr(this)'>aaa</a></td> <td><a href='#' onclick='removeTr(this)'>bbb</a></td> </tr> </table>
js code:
function removeTr(temp){ $(temp).parent().parent().remove(); //必须保证已经引入了jQuery,此处$(temp)先获取到<a>对象,.parent()拿到<td>,再.parent()获取到tr }
Recommended learning: "jquery video tutorial"
The above is the detailed content of How to remove tr in jquery. For more information, please follow other related articles on the PHP Chinese website!