简体   繁体   English

将数据属性添加到类下的所有图像

[英]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! 干杯!

This will do a trick 这将是一个技巧

Jquery jQuery的

$(document).ready(function() {

    // Scan the webpage for all class names of 'thumb' that is seen.

    $('.wrapper a').attr('data-lity', '');

});

在此输入图像描述

DEMO DEMO

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.

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