I need some simple JQuery code so I can change the src value of a specific img.
It's currently:
<img id="myImage" src="image1.gif" />
and I need to change it to:
<img id="myImage" src="image2.gif" />
using JQuery.
Using: $(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.
JS
$("#id1").fadeOut();
$("#id2").delay(200).fadeIn();
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).
这是基本的,使用jQuery attr ...
$('img#myImage').attr('src', 'image2.gif');
You could use the .attr()
function to set attributes on given DOM element:
$(function() {
$('#myImage').attr('src', 'image2.gif');
});
$('#myImage').attr('src','theothersrc.gif')
$("#myImage").attr('src', 'image2.gif')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.