我想将我的图像绑定到div标签。仅绑定到第一个对象而不是 所有。 我将图像存储在azure blob存储中。
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相同的id,即我用于div标签的MyProfile
答案 0 :(得分:0)
不要将ID选择器用于多个元素!
var $profiles = $('.MyProfile');
$profile.forEach(element, index) {
element.attr('src', "data:" + data.ContentType + ";base64," + data.Content);
}
答案 1 :(得分:0)
首先,使用一个类而不是ID,如$(' .bindImage')。attr(...)。
第二个,你得到多个元素,所以你需要经历它们:
var elements = $('.bindImage');
elements.forEach(element, index) {
element.attr('src', "data:" + data.ContentType + ";base64," + data.Content);
}
答案 2 :(得分:0)
您正在按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);
答案 3 :(得分:0)
css id选择器(#MyProfile
)仅返回它在页面上找到的第一个元素,因为ID选择器在页面上应该是唯一的。看起来你真的想要使用类选择器(.cssClassHere
)