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

时间:2017-11-15 13:51:06

标签: javascript css href

此幻灯片中有大约50张图片。我使用类似于wordpress的平台,并且不允许我更改图像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>

2 个答案:

答案 0 :(得分:2)

首先,图片没有href,他们有src

其次,当你说&#34;标题名称&#34;?

时,我认为你的意思是alt

在这种情况下,document.querySelector('[alt="the title"]').src = 'the new src';

如果您想通过链接标题找到它,则必须执行document.querySelector('a[title="the title"] > img').src = 'the new src';

答案 1 :(得分:0)

首先,我不确定您是否要更改a tagimg tag属性,但我会认为它是a tag,因为您提到href。是的。你可以这样做:

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

如果你想要图像:

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