The solution to the failure of jquery to delete tr: 1. Get the td object first through "$(temp)"; 2. Get the tr of td through ".parent()"; 3. Use "remove" ()" method to delete tr.
The operating environment of this article: windows7 system, jquery version 1.2.6, DELL G3 computer
What should I do if jquery fails to delete tr?
jQuery’s invalid solution for removing tr (tr is dynamically added)
When I was working on a project today, I encountered a problem, that is, removing certain tr (tr is dynamically added). 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 <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 }
Recommended learning: "jquery video tutorial"
The above is the detailed content of What to do if jquery fails to delete tr. For more information, please follow other related articles on the PHP Chinese website!