简体   繁体   中英

How to change image's href with Javascript by using title name?

Here in this slideshow there are around 50 images. I use a platform which is similar to wordpress and doesnt let me to change image href. The images have the same class name. Is it possible to use title name(which is unique for each image) and change some image's href?

<div id="slideshow" class="vlt-d-slider" style="position: relative; overflow: hidden; width: 928px; height: 522px;">
    <div class="slide" data-vltcaption="0" data-brand="none" data-topic="INST" data-url="_0.jpg" style="position: absolute; left: 0px; visibility: visible; z-index: 3; line-height: 520px; height: 522px;">
<a class="slide-lightbox" rel="vlt-slideshow" href="m_0.jpg" title="Pfusc number photo 1" target="_blank">
<img class="slide-img" src="_0.jpg" alt="Pfusc number photo 1">
</a>
</div>
      <div class="slide" data-vltcaption="1" data-brand="none" data-topic="INST" data-url="0.jpg" style="position: absolute; left: 0px; visibility: hidden; z-index: 1; line-height: 520px; display: block; height: 522px;">
<a class="slide-lightbox" rel="vlt-slideshow" href="f_0.jpg" title="Pfusc number photo 2" target="_blank">
<img class="slide-img" src="0.jpg" alt="Pfusc number photo 2">
</a>
</div>
      <div class="slide" ....
</div>
    </div>

Firstly, images don't have href , they have src .

Secondly, I think you mean alt when you say "title name"?

In that case, document.querySelector('[alt="the title"]').src = 'the new src';

If you want to find it by the title of the link, you would have to do document.querySelector('a[title="the title"] > img').src = 'the new src';

For start I'm not sure do you want to change your a tag or img tag properties, but I will presume it's a tag because of you mentioned href . Yes it is. You can do it like this:

$("a[title='yourTitle']").attr('href', 'https://www.google.com/');

and if you want image:

$('img[alt="Your Alt"]').attr('src', 'image.jpg');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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