Home  >  Article  >  Web Front-end  >  jQuery’s invalid solution for removing tr (tr is added dynamically)_jquery

jQuery’s invalid solution for removing tr (tr is added dynamically)_jquery

WBOY
WBOYOriginal
2016-05-16 16:35:531465browse

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 recorded it here.

$(temp).parent().remove(); //temp为td的id 
code class="js plain"> 我的理解是这样的:$(temp)先获取到该td对象,然后.parent()获取到td的tr,再remove()方法,删除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(); //必须保证页面已经引入了jQuery才可以使用 
//此处$(temp)先获取到<a>对象,.parent()拿到<td>,再.parent()获取到tr 
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn