使用 JavaScript 更改图像源 [英] Change image source with JavaScript
问题描述
所以我是 JavaScript 新手(这实际上是我第一次尝试做一些事情)并且我遇到了一些麻烦.我以为我有足够的知识来完成这项工作,我什至在谷歌上搜索了可以帮助我解决这个问题的教程和脚本,但没有任何帮助.
我似乎无法更改图像源,这是我目前拥有的代码:
function changeImage(a) {document.getElementById("img").src=a.src;}
<img id="img";src=1772031_29_b.jpg"><div id="thumb_img"><img src='1772031_29_t.jpg' onclick='changeImage("1772031_29_b.jpg");'><img src='1772031_55_t.jpg' onclick='changeImage("1772031_55_b.jpg");'><img src='1772031_53_t.jpg' onclick='changeImage("1772031_53_b.jpg");'>
如果我做错了什么,有人可以解释一下吗?或者也许我错过了什么?请帮助我:-)
function changeImage(a)
所以没有 a.src
=> 之类的东西就用a
.
演示 这里
So I'm new with JavaScript (this is actually my first attempt to make something work) and I'm having a bit of trouble. I thought I had enough knowledge to make this work, I've even googled for tutorials and scripts that could help me work this out but nothing really helped.
I can't seem to change the image source, heres the code that I have so far:
function changeImage(a) {
document.getElementById("img").src=a.src;
}
<div id="main_img">
<img id="img" src="1772031_29_b.jpg">
</div>
<div id="thumb_img">
<img src='1772031_29_t.jpg' onclick='changeImage("1772031_29_b.jpg");'>
<img src='1772031_55_t.jpg' onclick='changeImage("1772031_55_b.jpg");'>
<img src='1772031_53_t.jpg' onclick='changeImage("1772031_53_b.jpg");'>
</div>
Could anyone please explain if I'm doing something wrong? Or maybe I'm missing something? Help me please :-)
function changeImage(a)
so there is no such thing as a.src
=> just use a
.
demo here
这篇关于使用 JavaScript 更改图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!