简体   繁体   English

如何为div容器中的图像添加“ rel”属性?

[英]How to add a “rel” attribute to images from a div container?

I want javascript to add this rel attribute rel="lightbox[1]" to all images from a given container when the page loads... 我希望javascript在页面加载时将此rel属性rel =“ lightbox [1]”添加到给定容器中的所有图像中...

Is that possible? 那可能吗?

<div id="otherimg">
   <p><img src="" alt="" /></p>
   <p><img src="" alt="" /></p>
   <p><img src="" alt="" /></p>
   <p><img src="" alt="" /></p>
</div>
$(function() {
    $('#otherimg img').attr('rel', 'lightbox[1]')
})

this will add to only the img tags inside div with id otherimg 这只会添加ID为otherimg div内的img标签

$(function() {

$('div#otherimg img').attr("rel", "lightbox[1]")

})

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

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