从父元素而不是子元素中删除属性

时间:2019-02-28 13:47:09

标签: jquery

我想从第二个href中删除li属性,而不是子li中的li属性,该怎么做?

此刻,它正在从第二个li$('.primary-menu-1 li:nth-child(2) > a').removeAttr('href'); 中删除带有“麻醉呼吸系统”的属性。

我要去哪里错了?

这是我的jquery:

<ul class="primary-menu-1">
  <li>
    <a href="https://company-test.local/" class="menu-image-title-after"><span class="menu-image-title">Home</span></a>
  </li>
  <li>
    <a href="https://company-test.local/products" class="menu-image-title-after"><span class="menu-image-title">Products</span></a>
    <ul>
        <li id="menu-item-8486" class="menu-item menu-item-type-taxonomy menu-item-object-categories">
        <a href="https://company-test.local/categories/airway-management/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Airway Management</span>
        </a></li>
      <li>
        <a href="https://company-test.local/categories/breathing-filters/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Anaesthesia Breathing Systems</span>
        </a>
      </li>
      <li>
        <a href="https://company-test.local/categories/breathing-filters/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Anaesthesia Components</span>
        </a>
      </li>
      <li>
        <a href="https://company-test.local/categories/breathing-filters/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Breathing Filters</span>
        </a>
      </li>
      <li>
        <a href="https://company-test.local/categories/oxygen-aerosol-therapy/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Oxygen &amp; Aerosol Therapy</span>
        </a>
      </li>
      <li>
        <a href="https://company-test.local/categories/ventilator-breathing-systems/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Ventilator Breathing Systems</span>
        </a>
      </li>
      <li>
        <a href="https://company-test.local/categories/resuscitation/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Resuscitation</span>
        </a>
      </li>
      <li>
        <a href="https://company-test.local/categories/suction/" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Suction</span>
        </a>
      </li>
      <li>
        <a href="http://mycompany.com" class="menu-image-title-below menu-image-not-hovered">
          <span class="menu-image-title">Urology</span>
        </a>
      </li>
      <li class="product-up"><a href="#" class=""></a></li>
    </ul>
  </li>
</ul>

这是菜单的html(由Wordpress生成):

//*[contains(@class,"highcharts-line-series")]/*[@fill="#FF9700"]

2 个答案:

答案 0 :(得分:1)

您需要从以下位置更改选择:

$('.primary-menu-1 li:nth-child(2) > a')

收件人:

$('.primary-menu-1 > li:nth-child(2) > a')
  

Child Selector :(“父母>子女”)

选择所有属于子ul / li的具有 href 属性的 a 元素。因此,您可以获得第二个:

$('.primary-menu-1 > li:nth-child(2) > a').addClass('red');
.red {
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<ul class="primary-menu-1">
    <li>
        <a href="https://company-test.local/" class="menu-image-title-after"><span class="menu-image-title">Home</span></a>
    </li>
    <li>
        <a class="menu-image-title-after"><span class="menu-image-title">Products</span></a>
        <ul>
            <li id="menu-item-8486" class="menu-item menu-item-type-taxonomy menu-item-object-categories">
                <a href="https://company-test.local/categories/airway-management/" class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Airway Management</span>
                </a></li>
            <li>
                <a class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Anaesthesia Breathing Systems</span>
                </a>
            </li>
            <li>
                <a class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Anaesthesia Components</span>
                </a>
            </li>
            <li>
                <a href="https://company-test.local/categories/breathing-filters/" class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Breathing Filters</span>
                </a>
            </li>
            <li>
                <a href="https://company-test.local/categories/oxygen-aerosol-therapy/" class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Oxygen &amp; Aerosol Therapy</span>
                </a>
            </li>
            <li>
                <a href="https://company-test.local/categories/ventilator-breathing-systems/" class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Ventilator Breathing Systems</span>
                </a>
            </li>
            <li>
                <a href="https://company-test.local/categories/resuscitation/" class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Resuscitation</span>
                </a>
            </li>
            <li>
                <a href="https://company-test.local/categories/suction/" class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Suction</span>
                </a>
            </li>
            <li>
                <a href="http://mycompany.com" class="menu-image-title-below menu-image-not-hovered">
                    <span class="menu-image-title">Urology</span>
                </a>
            </li>
            <li class="product-up"><a href="#" class=""></a></li>
        </ul>
    </li>
</ul>

答案 1 :(得分:0)

尝试一下:

$('.primary-menu-1 > li:nth-child(2) > a').removeAttr('href');