javascript 从 AJAX 成功函数更新图像源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20113291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update Image source from AJAX success function
提问by Dilukshan Mahendra
I use to update Label values inside the AJAX success function like below, But I need to know how I'm going to apply this method to change/update "src" of an <img id="myimage" src=""/>
我用来更新 AJAX 成功函数内的标签值,如下所示,但我需要知道我将如何应用此方法来更改/更新 <img id="myimage" src=""/>
$.ajax({
url: 'clmcontrol_livematchupdate',
type: 'post',
dataType: 'json',
success: function (data) {
$('#mstatus').html(data.matchstatus);
// $('#myimage').... ?
},
complete: function () {
// Schedule the next request when the current one has been completed
setTimeout(ajaxInterval, 4000);
}
});
回答by Krish R
Using jquery, You can use like $("#myimage").attr('src','img url');
使用 jquery,你可以使用像 $("#myimage").attr('src','img url');
Assume, you have response like data.imgsrc
then it should be like, $("#myimage").attr(src, data.imgsrc);
假设,你的反应data.imgsrc
应该是这样的,$("#myimage").attr(src, data.imgsrc);
$.ajax({
url: 'clmcontrol_livematchupdate',
type: 'post',
dataType: 'json',
success: function (data) {
$('#mstatus').html(data.matchstatus);
$("#myimage").attr('src','img url');
},
complete: function () {
// Schedule the next request when the current one has been completed
setTimeout(ajaxInterval, 4000);
}
});
回答by swt
$('#myimage').attr('src', '/imagePath/');
回答by Tushar Gupta - curioustushar
Try .prop()
试试.prop()
success: function (data) {
$('#mstatus').html(data.matchstatus);
$('#myimage').prop('src', 'VAlue'); //change image src
}
读 .prop() vs .attr().prop() 与 .attr()