【问题标题】:removing parent href attributes of certain div elements删除某些 div 元素的父 href 属性
【发布时间】:2018-05-18 15:01:10
【问题描述】:

我有一个表包含包含锚元素的行 这个锚元素包含 div 元素,我想删除某些 anhcor 的“href”属性,其子 div 元素类包含“bg-color-green”。

这是 html 的外观;

<a href="somelink data-toggle="tooltip" title="" data-original-title="Tarih Aralığı :
01.02.2014 - 02.03.2014
   Uygulanma Tarihi : 
04.02.2014">
    <div class="well well-sm bg-color-green txt-color-white text-center">
        II
    </div>
</a>

并删除父锚的所有 href 属性,我使用它;

$(".bg-color-green").parent('a').remove('href')

但它不起作用,这是什么问题?

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    使用removeAttr("href") 而不是remove("href")

    另请注意,您在href="somelink 处缺少结尾"

    演示

    $(".bg-color-green").parent('a').removeAttr('href')
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="somelink" data-toggle=" tooltip " title=" " data-original-title="Tarih Aralığı : 01.02.2014 - 02.03.2014 Uygulanma Tarihi : 04.02.2014 ">
      <div class="well well-sm bg-color-green txt-color-white text-center ">
        II
      </div>
    </a>

    【讨论】:

      【解决方案2】:

      attr('href','') 设置href attr 为空。

      $(".bg-color-green").parent('a').attr('href','');
      console.log($(".bg-color-green").parent('a').attr('href'))
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <a href="somelink data-toggle="tooltip" title="" data-original-title="Tarih Aralığı :
      01.02.2014 - 02.03.2014
         Uygulanma Tarihi : 
      04.02.2014">
          <div class="well well-sm bg-color-green txt-color-white text-center">
              II
          </div>
      </a>

      或使用removeAttr() 函数将其删除。

      $(".bg-color-green").parent('a').removeAttr('href');
      console.log($(".bg-color-green").parent('a').attr('href'))
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <a href="somelink data-toggle="tooltip" title="" data-original-title="Tarih Aralığı :
      01.02.2014 - 02.03.2014
         Uygulanma Tarihi : 
      04.02.2014">
          <div class="well well-sm bg-color-green txt-color-white text-center">
              II
          </div>
      </a>

      【讨论】:

        猜你喜欢
        • 2022-08-23
        • 1970-01-01
        • 1970-01-01
        • 2022-08-08
        • 2013-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多