Home > Web Front-end > Front-end Q&A > How to remove tr in jquery

How to remove tr in jquery

藏色散人
Release: 2021-12-01 11:25:51
Original
3144 people have browsed it

Jquery method to remove tr: 1. Create a js sample file; 2. Introduce jQuery; 3. Use the "function removeTr(temp){...}" method to remove tr.

How to remove tr in jquery

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。
Copy after login

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=&#39;#&#39; onclick=&#39;removeTr(this)&#39;>123</a></td>
       <td><a href=&#39;#&#39; onclick=&#39;removeTr(this)&#39;>456</a></td>
            </tr>
   <tr>
       <td><a href=&#39;#&#39; onclick=&#39;removeTr(this)&#39;>aaa</a></td>
       <td><a href=&#39;#&#39; onclick=&#39;removeTr(this)&#39;>bbb</a></td>
   </tr>
        </table>
Copy after login

js code:

function removeTr(temp){
  $(temp).parent().parent().remove(); 
//必须保证已经引入了jQuery,此处$(temp)先获取到<a>对象,.parent()拿到<td>,再.parent()获取到tr
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template