使用 JavaScript 更改图像源
所以我是 JavaScript 新手(这实际上是我第一次尝试让某些东西发挥作用)并且我遇到了一些麻烦。我以为我有足够的知识来完成这项工作,我什至在谷歌上搜索了可以帮助我解决这个问题的教程和脚本,但没有任何真正的帮助。
我似乎无法更改图像源,这是我到目前为止所拥有的代码:
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>
如果我做错了什么,有人可以解释一下吗?或者也许我错过了什么?请帮助我:-)
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 :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
function changeImage(a)
所以不存在a.src
=>;只需使用a
。演示此处
function changeImage(a)
so there is no such thing asa.src
=> just usea
.demo here
如果您始终将模式放在
_b
而不是_t
上,您可以通过传递对图像本身的引用来使其更通用:然后在函数中:
If you will always have the pattern on
_b
instead of_t
you can make it more generic by passing reference to the image itself:Then in the function:
我也管这个问题。但是每次更改源(图像)时都通过图像的实例来解决它。
似乎只会调用一次 onload 。
但这样,您就可以随时更改图像。
模式使用。
I also tube that problem. But solve it by an instance of an image every time you change the source (image).
It seems that would be called onload only once.
But this way, you can change image whenever you want.
Mode use.
您唯一真正的问题是您传递的是字符串,而不是具有
.src
属性的对象一些建议:
data-
前缀属性标记:
JavaScript:
如果您需要支持 IE 和其他旧版浏览器,请使用正确的 polyfill for Array.from
编辑:适用于现代浏览器的 Vanilla JS。
Your only real problem is you are passing a string, not an object with a
.src
propertySome suggestions:
<button>
data-
prefixed attributes for event data on the elementMarkup:
JavaScript:
If you need to support IE and other legacy browsers, Use a proper polyfill for Array.from
Edit: Vanilla JS for modern browsers.
您已经进行了一些更改(假设您确实仍然想要更改 ID 为 IMG 的图像,如果不使用 Shadow Wizard 的解决方案)。
删除
a.src
并替换为a
:更改
onclick
属性以包含新图像源的字符串而不是文字:You've got a few changes (this assumes you indeed still want to change the image with an ID of IMG, if not use Shadow Wizard's solution).
Remove
a.src
and replace witha
:Change your
onclick
attributes to include a string of the new image source instead of a literal:问题是您在不需要它的情况下使用了
.src
并且您还需要区分要更改的
img
。The problem was that you were using
.src
without needing it andyou also needed to differentiate which
img
you wanted to change.以下示例程序用于每 100 毫秒更改一次图像 src 属性。您可以根据需要调用给定的函数。
The Following Example Program used to change the image src attribute for every 100 milliseconds. you may call the given function as your wish.
不要写这个,而是
尝试:
Instead of writing this,
try:
我知道这个问题很旧,但是对于新问题,您可以执行以下操作:
HTML
JAVASCRIPT
I know this question is old, but for the one's what are new, here is what you can do:
HTML
JAVASCRIPT
使用上面的代码,将此html文件和图片(注意命名,因为我已经用我的图片名称给出了上面的代码)在同一个文件夹中
你会得到...
Use the above code by placing this html file and pics(take care in namings, beacause I have given the above code with my pic names) in same folder
you will get...
试试这个!
Try this!
首先我们显示这个 image1:
onclick 全屏显示图像
,我们有其他 image2
onclick 我们用 image2 替换 image1
first we have this image1 displayed:
onclick to display the image full screen
and we have other image2
onclick we replace image1 by image2