在一组图像中仅使用javascript更改src

在一组图像中仅使用javascript更改src,javascript,image,src,Javascript,Image,Src,我对JavaScript非常陌生。如果我单击一个新的图像,该图像将改变SRC和其他图像 代码如下: <a href="#pduno"><img onclick="this.src='img/pduno.png';document.getElementsByClassName('imgdp').src='img/pddos.png'" class="imgdp" src="img/pduno.png" width="13" height="11" /></a>

我对JavaScript非常陌生。如果我单击一个新的图像,该图像将改变SRC和其他图像

代码如下:

<a href="#pduno"><img onclick="this.src='img/pduno.png';document.getElementsByClassName('imgdp').src='img/pddos.png'" class="imgdp" src="img/pduno.png" width="13" height="11" /></a> 
<a href="#pddos"><img onclick="this.src='img/pduno.png';document.getElementsByClassName('imgdp').src='img/pddos.png'" class="imgdp" src="img/pddos.png" width="13" height="11" /></a>
<a href="#pddos"><img onclick="this.src='img/pduno.png';document.getElementsByClassName('imgdp').src='img/pddos.png'" class="imgdp" src="img/pddos.png" width="13" height="11" /></a>

pduno.png是“活动”图像,pddos.png是“非活动”图像

让我们想象一下,我有3个图像pduno-pddos-pddos

当我单击两个PDDO中的一个时,它将变为pduno,而原来的pduno将变为pddos。我的意思是,只有一个带有pduno的图像,而其余的都是PDDO


我用这个来创建一个滚动画廊。pduno用于显示正在显示的库。

文档。GetElementsByCassName
将从文档中选择节点列表,而不是单个元素。要更改每个选定元素的src属性,必须在列表上循环


另外,要将所有图像重置为PDDO,然后激活一个图像,您不能将一个图像设置为pduno,然后重置所有图像。

文档。getElementsByClassName
将从文档中选择节点列表,而不是单个元素。要更改每个选定元素的src属性,必须在列表上循环

另外,要将所有图像重置为PDDO,然后激活其中一个图像,您不能将其中一个图像设置为pduno,然后重置所有图像。

我会使用该库(因为您需要使用一些不太简单的函数)

您可以将其包含在编写此代码中(无需下载任何内容):


然后,我会这样做:

<a href="#pddos"><img class="pdimg" src="img/pduno.png" width="13" height="11" /></a>
<a href="#pddos"><img class="pdimg" src="img/pddos.png" width="13" height="11" /></a>
<a href="#pddos"><img class="pdimg" src="img/pddos.png" width="13" height="11" /></a>

在HTML中,我删除了所有脚本,并将它们移动到此处:

<script>
$('.pdimg').click(function(){ //This registers the function with the click event
    $('.pdimg').attr('src', 'img/pddos.png'); //This resets the image to pddos
    $(this).attr('src', 'img/pddos.png'); //This sets the image to uno, 
                             // "this" will be the img that you clicked on.
}
</script>

$('.pdimg')。单击(function(){//这将向单击事件注册函数
$('.pdimg').attr('src','img/pddos.png');//这会将图像重置为pddos
$(this.attr('src','img/pddos.png');//这将图像设置为uno,
//“this”将是您单击的img。
}
我会使用这个库(因为您需要使用一些不太简单的函数)

您可以将其包含在编写此代码中(无需下载任何内容):


然后,我会这样做:

<a href="#pddos"><img class="pdimg" src="img/pduno.png" width="13" height="11" /></a>
<a href="#pddos"><img class="pdimg" src="img/pddos.png" width="13" height="11" /></a>
<a href="#pddos"><img class="pdimg" src="img/pddos.png" width="13" height="11" /></a>

在HTML中,我删除了所有脚本,并将它们移动到此处:

<script>
$('.pdimg').click(function(){ //This registers the function with the click event
    $('.pdimg').attr('src', 'img/pddos.png'); //This resets the image to pddos
    $(this).attr('src', 'img/pddos.png'); //This sets the image to uno, 
                             // "this" will be the img that you clicked on.
}
</script>

$('.pdimg')。单击(function(){//这将向单击事件注册函数
$('.pdimg').attr('src','img/pddos.png');//这会将图像重置为pddos
$(this.attr('src','img/pddos.png');//这将图像设置为uno,
//“this”将是您单击的img。
}