简体   繁体   English

如何使用标题名称使用Javascript更改图片的href?

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

Here in this slideshow there are around 50 images. 在此幻灯片显示中,大约有50张图像。 I use a platform which is similar to wordpress and doesnt let me to change image href. 我使用类似于wordpress的平台,但不允许我更改图片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? 是否可以使用标题名称(每个图像唯一)并更改某些图像的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 . 首先,图像没有href ,它们具有src

Secondly, I think you mean alt when you say "title name"? 其次,我认为当您说“标题名称”时您指的是alt吗?

In that case, document.querySelector('[alt="the title"]').src = 'the new src'; 在这种情况下, 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'; 如果要通过链接的标题查找它,则必须执行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 . 首先,我不确定您是否要更改a tagimg tag属性,但由于您提到href ,因此我认为它a tag 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');

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

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