【问题标题】:Jquery remove attribute from element before currentJquery在当前元素之前从元素中删除属性
【发布时间】:2016-03-21 17:42:54
【问题描述】:

这是我的代码

<a target="_blank" href="/pics/2016/03/21/56efb593855ae56efb5938599556efb59385d80.jpg" title="Click For Larger View">
 <img src="/pics/2016/03/21/56efb593855ae56efb5938599556efb59385d80.jpg" width="150px" height="150px" id="post-img" class="post-img">
</a>

jQuery 中有没有办法从.post-img 类上方的元素中删除href 元素。

所以理论上我希望它变成这样:

<a target="_blank" title="Click For Larger View">
 <img src="/pics/2016/03/21/56efb593855ae56efb5938599556efb59385d80.jpg" width="150px" height="150px" id="post-img" class="post-img">
</a>

这是可以做到的;只需将元素的类 (.post-img) 放在 a(link) 中即可。

!!!无论如何可以这样做!!!

请你帮帮我,我不太擅长JavascriptJquery!!!

【问题讨论】:

标签: javascript jquery html jquery-ui


【解决方案1】:
 // select the parent of img with parent()
 var a = $('img.post-img').parent();
 a.removeAttr('href');

https://jsfiddle.net/sra9sndm/

【讨论】:

    【解决方案2】:

    使用parent() 方法获取父级,然后使用removeAttr() 删除href。

    $('.post-img').parent().removeAttr('href');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a target="_blank" title="Click For Larger View" href="/pics/2016/03/21/56efb593855ae56efb5938599556efb59385d80.jpg">
        <img src="/pics/2016/03/21/56efb593855ae56efb5938599556efb59385d80.jpg" width="150px" height="150px" id="post-img" class="post-img">
    </a>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2011-03-09
      • 2014-04-29
      • 2021-03-02
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      相关资源
      最近更新 更多