Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript更改图像源(Change image source with JavaScript)

So I'm new with JavaScript (this is actually my first attempt to make something work) and I'm having a bit of trouble.

(所以我是JavaScript的新手(这实际上是我第一次尝试做某事)并且我遇到了一些麻烦。)

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.

(我认为我有足够的知识来完成这项工作,我甚至用Google搜索了可以帮助我解决问题的教程和脚本,但没有任何帮助。)

I can't seem to change the image source, heres the code that I have so far:

(我似乎无法更改图像源,这是我到目前为止的代码:)

<script type="text/javascript">
    function changeImage(a) {
        document.getElementById("img").src=a.src;
    }
</script>
<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 :-)

(请帮帮我 :-))

  ask by Alvaro Arregui translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

function changeImage(a) so there is no such thing as a.src => just use a .

(function changeImage(a)所以没有a.src =>这样的东西只是使用a 。)

demo here

(在这里演示)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...