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
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.