简体   繁体   中英

jQuery remove all li elements with empty a childs

How do I remove all <li></li> elements with an empty <a></a> tag?

Example:

<li><a href=''>List item</a></li> //Do not delete
<li><a href=''></a></li> //Delete

You could combine the :has() / :empty jQuery selectors:

Example Here

$('li:has(a:empty)').remove();

Use following script

$("li a:empty").parent().remove();

For reference - https://api.jquery.com/empty/

To only remove li elements with empty a children use this:

$("li > a:empty").parent().remove();
//OR $('li:has(>a:empty)').remove();

Examples:

<li><a href=''>List item</a></li> <!-- not remove this -->
<li><a href=''></a></li> <!-- remove this -->
<li><span><a href=''></a></span></li> <!-- not remove this -->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM