[英]Add data attribute to all images under a class
I'm using a lightbox that requires a specific tag inside the link. 我正在使用一个需要链接中特定标签的灯箱。 I don't really want to edit every post so I'm trying to do this automatically using jquery.
我真的不想编辑每个帖子所以我试图使用jquery自动执行此操作。
HTML HTML
<div class="wrapper">
<a href="imagelink.png">Image</a>
</div>
JS JS
$(document).ready(function() {
// Scan classes
$('.wrapper a').each(function(){
// Apply tag
$(this).parent().attr('data-lity');
});
});
Result should be 结果应该是
<div class="wrapper">
<a href="imagelink.png" data-lity>Image</a>
</div>
JSfiddle http://jsfiddle.net/c2RvG/31/ JSfiddle http://jsfiddle.net/c2RvG/31/
script 脚本
$('.wrapper a').attr('data-lity', '');
You don't need to iterate. 您不需要迭代。 Jquery does it for you.
Jquery为你做到了。 Just set the attribute.
只需设置属性即可。
Hope it helps. 希望能帮助到你。 Cheers!
干杯!
Try this 试试这个
$(document).ready(function() {
// Scan classes
$('.wrapper a').each(function(){
// Apply tag
$(this).parent().attr('data-lity','');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.