簡體   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