Jquery 在当前之前从元素中删除属性

Jquery remove attribute from element before current

本文关键字:删除 属性 元素 Jquery      更新时间:2023-09-26

这是我的代码

<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(链接)内即可。

!!但是,这可以做到

吗!!

请你帮帮我,我对JavascriptJquery !!!不太好

 // select the parent of img with parent()
 var a = $('img.post-img').parent();
 a.removeAttr('href');

https://jsfiddle.net/sra9sndm/

使用

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>