简体   繁体   English

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

[英]Jquery remove attribute from element before current

Here Is My Code这是我的代码

<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>

Is there a way in jQuery to to get the remove the href element from the element above the .post-img class. jQuery 中有没有办法从.post-img类上方的元素中删除href元素。

So in theory I want it to turn out like:所以理论上我希望它变成这样:

<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>

and this can be done;这是可以做到的; just by having the class ( .post-img ) of the element inside of the a (link).只需将元素的类( .post-img )放在a (链接)中。

!!!HOWEVER CAN THIS BE DONE!!! !!!但是这可以做到!!!

Please could you help me I am not that good with Javascript or Jquery !!!请你能帮我吗 我不太擅长JavascriptJquery !!!

Get the parent using parent() method and then remove href using removeAttr() .使用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>

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

https://jsfiddle.net/sra9sndm/ https://jsfiddle.net/sra9sndm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM