日期:2014-05-16  浏览次数:20388 次

怎么判断AJAX取回来的DOM树数据中的图片等信息已经全部加载完成
怎么判断AJAX取回来的DOM树数据中的图片等信息已经全部加载完成,因为现在有个需求要在插入DOM树后取得该对象的scrollHeight高度值,如果里面的图片还没有加载完成就取则不能得到准确的值。求高手帮忙解答,跪谢!!!

------解决方案--------------------
JScript code
var imgList = $(yourDom).find('img');

var callback = function(){
  alert('全部图片下载完毕!');
};

var check = (funciton(count, fn){
  return function(){
    count--;
    if(count == 0){
       fn();
    }
  };
})(imgList.length, callback);

imgList.each(function(){
  $(this).bind('load', check)
         .attr(src, $(this).attr('src'));
});

------解决方案--------------------
修正楼上的一些BUG!拼写错误和字符漏写等,并补充一下代码顺序和结构!此方法可行!鉴定OVER!
HTML code
<html>
<head>
<script src="jq.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
var imgList = $('#ccc').find('img');
//alert(imgList.length);
var callback = function(){
  alert('全部图片下载完毕!');
};

var check = (function (count, fn){
  return function(){
    count--;
    if(count == 0){
       fn();
    }
  };
})(imgList.length, callback);

imgList.each(function(){
  $(this).bind('load', check)
         .attr('src', $(this).attr('src'));
});
});
</script>
</head>
<body id='ccc'>
<img src="http://pic8.nipic.com/20100720/145234_090537348485_2.jpg"/>
<img src="http://pic1a.nipic.com/2009-02-25/2009225211211141_2.jpg"/>
<img src="http://pic4.nipic.com/20091113/213291_152442048094_2.jpg"/>
<img src="http://pic3.nipic.com/20090512/1813115_005630566_2.jpg"/>
<img src="http://pic4.nipic.com/20091113/1982774_180252868907_2.jpg"/>
<img src="http://junshi.xiansheng.com/uploadfile/2010/1003/20101003103341594.jpg"/>
<img src="http://pic1a.nipic.com/2008-09-19/200891922637887_2.jpg"/>
<img src="http://pic4.nipic.com/20091205/1295091_185150911309_2.jpg"/>
<img src="http://pic1a.nipic.com/2009-02-25/2009225211127244_2.jpg"/>
<img src="http://pic5.nipic.com/20091229/3374535_001959343200_2.jpg"/>
</body>
</html>