简体   繁体   English

需要帮助,使用js将rel添加到img标签

[英]Need help adding rel to an img tag using js

I need help with some JS. 我需要一些JS的帮助。 I need to add rel="lightbox" to the main images of the code below so that these images will open in a lightbox. 我需要将rel="lightbox"添加到下面的代码的主图像中,以便这些图像可以在灯箱中打开。 I grabbed this code from the inspector and this is being generated by a plugin so instead of editing the plugin files I'd like to add the rel code another way. 我从检查器中获取了这段代码,它是由插件生成的,因此我不想编辑插件文件,而是想以其他方式添加rel代码。 I assume I can add this via JS but nothing I am finding is working. 我想我可以通过JS添加它,但是我发现没有任何工作。 Any help would be appreciated. 任何帮助,将不胜感激。

<div class="rsOverflow" style="width: 786px; height: 544px;">
    <div class="rsContainer">
        <div style="z-index:0;" class="rsSlide ">
            <div class="rsContent">
                <img class="rsImg rsMainSlideImage" src="http://166.62.38.87/~saphotonics/wp-content/uploads/2019/05/SA-62H_image1-1024x894.jpg" style="width: 555px; height: 484px; margin-left: 115px; margin-top: 30px;">
             </div>
        </div>
        <div style="z-index:0; display:none; opacity:0;" class="rsSlide ">
            <div class="rsContent">
                <img class="rsImg rsMainSlideImage" src="http://166.62.38.87/~saphotonics/wp-content/uploads/2019/05/SA-62H_image2-1008x1024.jpg" style="width: 477px; height: 484px; margin-left: 154px; margin-top: 30px;">
            </div>
        </div>
    </div>
    <div class="rsFullscreenBtn">
        <div class="rsFullscreenIcn"></div>
    </div>
</div>

You need to do something like this: 您需要执行以下操作:

const imgs = document.querySelectorAll('img.rsImg')

for(let img of imgs){
  img.setAttribute('rel', 'lightbox');
}

The code is not tested, but I'm sure it'll guide you. 该代码未经测试,但是我相信它将为您提供指导。

It looks like all of the images you want are tagged with the class "rsImg". 看起来您想要的所有图像都被标记为“ rsImg”类。
If this is the case, you can select all images with that class and add the "rel" attribute to them... 如果是这种情况,则可以选择该类别的所有图像,然后向其添加“ rel”属性。

document.querySelectorAll('img.rsImg') // selects all "img" tags that have class "rsImg"
        .forEach(                      // for each of the things selected, run the code here
                 n => n.setAttribute('rel', 'lightbox')
                )

The following snippet runs that code, using your html from the question (with the images changed to placeholders) -- after it runs you can use your browser to inspect the element(s) and see that the rel attribute is added to each img . 以下代码段使用问题中的html来运行该代码(图像更改为占位符)-运行后,您可以使用浏览器检查元素,并查看rel属性是否已添加到每个img

 document.querySelectorAll('img.rsImg').forEach(n => n.setAttribute('rel', 'lightbox')) 
 <div class="rsOverflow" style="width: 786px; height: 544px;"> <div class="rsContainer"> <div style="z-index:0;" class="rsSlide "> <div class="rsContent"> <img class="rsImg rsMainSlideImage" src="//www.fillmurray.com/1024/894" style="width: 555px; height: 484px; margin-left: 115px; margin-top: 30px;"> </div> </div> <div style="z-index:0; display:none; opacity:0;" class="rsSlide "> <div class="rsContent"> <img class="rsImg rsMainSlideImage" src=" //www.fillmurray.com/1008/1024" style="width: 477px; height: 484px; margin-left: 154px; margin-top: 30px;"> </div> </div> </div> <div class="rsFullscreenBtn"> <div class="rsFullscreenIcn"></div> </div> </div> 

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

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