我想将我的映像绑定到div tag.it,只绑定到第一个对象,而不是所有对象。我把图像存储在天蓝色的水珠存储器里。
type: "Get",
url: "/contollerName/methodname",
success: function (result) {
data = JSON.parse(result);
alert("Pic " + data.ContentType +" "+data.Content);
$('#MyProfile').attr('src', "data:" + data.ContentType + ";base64," + data.Content);
此方法为image.and提供了与div标记相同的id,即MyProfile。
发布于 2016-02-22 09:49:34
您正在按ID引用div。在有效的HTML中,每页只能使用一次ID。将其更改为类并在jquery中引用它。虽然div没有src属性,但是要引用图像:
<div class="profilePic"><img src="value.png"/></div>
jquery部分:
$('.profilePic img').attr('src', "data:" + data.ContentType + ";base64," + data.Content);
发布于 2016-02-22 09:48:49
不要对多个元素使用ID选择器!
var $profiles = $('.MyProfile');
$profile.forEach(element, index) {
element.attr('src', "data:" + data.ContentType + ";base64," + data.Content);
}
发布于 2016-02-22 09:48:51
首先,使用类而不是ID,比如$(‘.bindImage’).attr(.)。
第二,您将获得多个元素,因此您需要检查它们:
var elements = $('.bindImage');
elements.forEach(element, index) {
element.attr('src', "data:" + data.ContentType + ";base64," + data.Content);
}
https://stackoverflow.com/questions/35550356
复制相似问题