繁体   English   中英

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

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

在此幻灯片显示中,大约有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>

首先,图像没有href ,它们具有src

其次,我认为当您说“标题名称”时您指的是alt吗?

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

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

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

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

并且如果您想要图像:

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

暂无
暂无

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

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