如何更改图像';通过使用标题名使用Javascript创建s href?
在本幻灯片中,大约有50幅图像。我使用的平台与wordpress类似,不允许我更改image href。这些图像具有相同的类名。是否可以使用标题名(每个图像的标题名都是唯一的)并更改一些图像的href如何更改图像';通过使用标题名使用Javascript创建s href?,javascript,css,href,Javascript,Css,Href,在本幻灯片中,大约有50幅图像。我使用的平台与wordpress类似,不允许我更改image href。这些图像具有相同的类名。是否可以使用标题名(每个图像的标题名都是唯一的)并更改一些图像的href <div id="slideshow" class="vlt-d-slider" style="position: relative; overflow: hidden; width: 928px; height: 522px;"> <div class="slide"
<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=“thetitle”]').src='thenewsrc'代码>
如果您想通过链接的标题找到它,您必须执行document.querySelector('a[title=“the title”]>img')。src='the new src'代码>首先,图像没有href
,它们有src
第二,我想你说的“标题名”是指alt
在这种情况下,document.querySelector('[alt=“thetitle”]').src='thenewsrc'代码>
如果您想通过链接的标题找到它,您必须执行document.querySelector('a[title=“the title”]>img')。src='the new src'
对于start,我不确定您是否要更改标记
或img标记
属性,但我假定它是标记
,因为您提到了href
。是的。您可以这样做:
$("a[title='yourTitle']").attr('href', 'https://www.google.com/');
如果您想要图像:
$('img[alt="Your Alt"]').attr('src', 'image.jpg');
首先,我不确定您是否要更改a标记
或img标记
属性,但由于您提到href
,我假定它是a标记
。是的。您可以这样做:
$("a[title='yourTitle']").attr('href', 'https://www.google.com/');
如果您想要图像:
$('img[alt="Your Alt"]').attr('src', 'image.jpg');
你是指按标题显示的alt属性吗?你是指按标题显示的alt属性吗?你是说,jQuery是可用的,甚至没有提到jQuery。你是对的,如果OP需要,我会调整代码。你是说,jQuery是可用的,甚至不提jQuery。你是对的,如果OP需要香草,我会调整代码。你可以使用('a[title=“the title”]>img')
并避免使用第二个查询选择器来简化代码。谢谢,@funcoding编辑了它!哎呀,你应该马上看一看这些基础知识;-)您可以使用('a[title=“the title”]>img')
并避免使用第二个查询选择器来简化它。谢谢,@funcoding编辑了它!哎呀,你应该马上看一看这些基础知识;-)