JQuery remove tr invalid solution of of tr is added dynamically

  • 2020-03-30 04:00:10
  • OfStack

Today, on a project, I had a problem removing some tr (tr is dynamically added). A number of approaches have been tried and failed (for example, the deleteRow method, which seems to pass only the number of rows in tr. Not carefully studied at present). Later, this method was found to be effective, and is hereby recorded.


$(temp).parent().remove(); //Temp is the id of td
code class="js plain">  My understanding is this: $(temp) Get that first td Object, and then .parent() Access to the td the tr Again, remove() Method, delete tr . </code>

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){ 
mp).parent().parent().remove(); //You must make sure that jQuery is introduced to the page before you can use it
//Here $(temp) gets first <A> Object,.parent() get <Td> ,.parent() gets tr
}

Related articles: