You can use jQuery's attr() function.
(您可以使用jQuery的attr()函数。)
For example, if your img
tag has an id
attribute of 'my_image', you would do this: (例如,如果您的img
标签的id
属性为“ my_image”,则可以执行以下操作:)
<img id="my_image" src="first.jpg"/>
Then you can change the src
of your image with jQuery like this:
(然后您可以使用jQuery更改图像的src
,如下所示:)
$("#my_image").attr("src","second.jpg");
To attach this to a click
event, you could write:
(要将其附加到click
事件,您可以编写:)
$('#my_image').on({
'click': function(){
$('#my_image').attr('src','second.jpg');
}
});
To rotate the image, you could do this:
(要旋转图像,可以执行以下操作:)
$('img').on({
'click': function() {
var src = ($(this).attr('src') === 'img1_on.jpg')
? 'img2_on.jpg'
: 'img1_on.jpg';
$(this).attr('src', src);
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…