简体   繁体   中英

Append HTML if parent has child

So I have a dropdown menu and I want to append some HTML to one of the

  • if it has a dropdown inside it.

    For example:

     <ul> <li>No Dropdown</li> <li>Dropdown Exists ** APPEND SOME HTML ** <ul> <li>This</li> </ul> </li> </ul> 

    So I know I can use this code:

     if ($('ul li').find("ul").length) { } 

    That will let me find out if the list item has a sub ul. But, how do I then target that specific list item so I can append some HTML in to it. I can't use the following for obvious reasons:

     if ($('nav ul li').find("ul").length) { $(this).*appendsomehtml* } 
  • 感谢Josh:

    $('nav ul li ul').parent().children('a').append('<i class="fa fa-caret-down"></i>');
    
        $('ul li').find('ul').each(function(){
          $(this).append("Html code");
        )};
    

    I think this is what you're looking for ^^

    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