当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Image source late binding - get rid of redundant error icon displayed in the meantime

问题描述:

On the page I have img declaration:

<img src="" alt="" itemid="7" />

I want to bind this image with appropriate source url. This url I need to fetch from external service:

$(document).ready(function () {

var id = $(img).attr('itemid');

getImageUrl(id); // fetch the image source url, takes about 5 seconds

});

function getImageUrl(id) {

$.ajax({

url: 'http://images.service.svc/get?id=' + id,

type: 'GET',

dataType: 'json',

contentType: 'application/json; charset=utf-8',

success:

/* after about 5 seconds */

function (result) {

var imageUrl = result.ImageUrl;

image.attr('src', imageUrl);

}

});

}

Meantime, in IE I get error image

How to show the image after source url is binded to image, next it is downloaded by the browser and finally ready to display ?

网友答案:

You could set the image to visibility:hidden in the CSS, then in your JavaScript, after the attribute is finished, set the CSS to visibility:visible

image.css('visibility','visible');
分享给朋友:
您可能感兴趣的文章:
随机阅读: