如果没有在具有相同类名的多个父元素中添加元素,则检查元素是否存在

时间:2020-04-25 14:51:17

标签: javascript html dom

我有一个多级菜单,一些菜单项包含带有菜单图标的标签,但是有些菜单项没有标签。所以我想使用Javascript查找所有父div。

我使用多种方法来查找所有元素,但失败了。

菜单列表代码:

'''

<ul class="hs-menu-children-wrapper">
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><span class="img"><div class="icon_sprite icon_domains"></div></span><a href="https://www.xcellhost.cloud/domains" role="menuitem">Domains<span class="sale badge">Sale</span></a></li>
    <li class="hs-menu-item hs-menu-depth-2 hs-item-has-children" role="menuitem"><span class="img"><div class="icon_sprite icon_web_hosting"></div></span><a href="/maintainance" role="menuitem">Web Hosting<span class="sale badge">Sale</span></a> <div class="child-trigger"><i></i></div>
     <ul class="hs-menu-children-wrapper">
      <li class="hs-menu-item hs-menu-depth-3" role="menuitem"><span class="img"><div class="icon_sprite icon_shared_web_hosting"></div></span><a href="https://www.xcellhost.cloud/shared-web-hosting" role="menuitem">Shared Web Hosting</a></li>
      <li class="hs-menu-item hs-menu-depth-3" role="menuitem"><span class="img"><div class="icon_sprite icon_managed_word_press"></div></span><a href="https://www.xcellhost.cloud/managed-wordpress" role="menuitem">Managed Wordpress <span class="hot badge">Hot</span></a></li>
      <li class="hs-menu-item hs-menu-depth-3" role="menuitem"><span class="img"><div class="icon_sprite icon_managed_woocomerce"></div></span><a href="https://www.xcellhost.cloud/woo-commerce" role="menuitem">Managed WooCommerce</a></li>
      <li class="hs-menu-item hs-menu-depth-3" role="menuitem"><span class="img"><div class="icon_sprite icon_managed_magneto"></div></span><a href="https://www.xcellhost.cloud/managed-magento" role="menuitem">Managed Magento</a></li>
      <li class="hs-menu-item hs-menu-depth-3" role="menuitem"><span class="img"><div class="icon_sprite icon_plesk"></div></span><a href="https://www.xcellhost.cloud/plesk-control-panel" role="menuitem">Plesk Control Panel</a></li>
     </ul></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><span class="img"><div class="icon_sprite icon_tally_on_cloud"></div></span><a href="https://www.xcellhost.cloud/tally-on-cloud" role="menuitem">Tally Cloud<span class="offer badge">Offer</span> </a></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><span class="img"><div class="icon_sprite icon_cloud_drive"></div></span><a href="https://www.xcellhost.cloud/cloud-drive" role="menuitem">Cloud Drive</a></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><a href="https://www.xcellhost.cloud/smb-cloud-desktop" role="menuitem">SMB Desktop Cloud<span class="hot badge">Recommended</span></a></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><span class="img"><div class="icon_sprite icon_ssl_certificate"></div></span><a href="https://www.xcellhost.cloud/ssl-certificate" role="menuitem">SSL Certificates<span class="sale badge">Sale</span> </a></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><span class="img"><div class="icon_sprite icon_website_security"></div></span><a href="https://www.xcellhost.cloud/cwatch" role="menuitem">Website Security <span class="hot badge">Recommended</span></a></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><a href="https://www.xcellhost.cloud/secure-web-gateway" role="menuitem">Secure Internet Access<span class="hot badge">Recommended</span></a></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><span class="img"><div class="icon_sprite icon_email_marketing"></div></span><a href="https://www.xcellhost.cloud/email-marketing" role="menuitem">Email Marketing</a></li>
    <li class="hs-menu-item hs-menu-depth-2" role="menuitem"><a href="https://www.xcellhost.cloud/employee-monitoring" role="menuitem">Employee Monitoring</a></li>
   </ul>
'

我尝试过的操作:-

if($('.vertical-menu-left ul ul li span#img').length == 0){
    $('.vertical-menu-left ul ul li a').before('<span class="img"><div class="icon_sprite icon_gpu_on_cloud"></div></span>');
}

0 个答案:

没有答案