[英]JQuery change the value of an <img src=“” by ID
I need some simple JQuery code so I can change the src value of a specific img. 我需要一些简单的JQuery代码,所以我可以更改特定img的src值。
It's currently: 它目前是:
<img id="myImage" src="image1.gif" />
and I need to change it to: 我需要将其更改为:
<img id="myImage" src="image2.gif" />
using JQuery. 使用JQuery。
Using: $(function(){ ... });
使用:
$(function(){ ... });
You can use: 您可以使用:
$('#id').attr('src', 'newImage.jpg');
to change the image source immediately. 立即更改图像源。
Alternatively, you can use jQuery animations to change an image. 或者,您可以使用jQuery动画来更改图像。
JS JS
$("#id1").fadeOut();
$("#id2").delay(200).fadeIn();
HTML HTML
<div>
<img id='id1' src='one.jpg'>
<img id='id2' src='two.jpg'>
</div>
(Don't forget to change the CSS of #id2
and put display: none
as initial state). (不要忘记更改
#id2
的CSS并将display: none
作为初始状态)。
这是基本的,使用jQuery attr ...
$('img#myImage').attr('src', 'image2.gif');
$('#myImage').attr('src','theothersrc.gif')
$("#myImage").attr('src', 'image2.gif')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.