关于一个获取数据问题

补全图片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        html,body{margin:0;padding:0;}
        .animationbox{width:500px;margin:10px auto;}
        .animationbox .uu{width:200px;height:200px;margin:10px;float:left;border:1px solid #ccc}
        .animationbox img{float:left;}
    </style>
</head>
<body>
    
    <div class="animationbox">
        <div class="uu">
            <img src="http://file3.u148.net/2011/4/images/1302139153715.jpg" width="100" height="100"/>
            <img src="http://file3.u148.net/2011/4/images/1302139115130.jpg" width="100" height="100"/>
            <img src="http://file3.u148.net/2011/4/images/1302139127832.jpg" width="100" height="100"/>
            <img src="http://file3.u148.net/2011/4/images/1302139131446.jpg" width="100" height="100"/>
        </div>
        <div class="uu">
            <img src="http://file3.u148.net/2011/4/images/1302139153715.jpg" width="100" height="100"/>
            <img src="http://file3.u148.net/2011/4/images/1302139115130.jpg" width="100" height="100"/>
        </div>
    </div>
    <script src="http://cdn.bootcss.com/jquery/2.2.1/jquery.js"></script>
    <script>

        $(function() {
            
        });
                
    </script>
      
</body>
</html>
阅读 2.6k
4 个回答
 $(function() {
       var uus =  $("DIV.uu");
        var totoalNum = 4;
       for(var i =0 ;i <uus.length;i++){
          var childreNum =  uus[i].children.length
           if(childreNum < totoalNum){
               var fillNum = totoalNum-childreNum;
               for(var j =0 ;j < fillNum;j++){
                  $(uus[i]).append('<img src="http://file3.u148.net/2011/4/images/1302139115130.jpg" width="100" height="100"/>');
               }
           }
       }

    });
$(document).ready(function(){
    $.each($('div.uu'), function(item, val){
        var num = 4 - $(val).find('img').length;
          $(val).append($(appendImgs(num)));
    });
});

function appendImgs(num){
    var img = '<img src="http://file3.u148.net/2011/4/images/1302139115130.jpg" alt="" title="" width="100" height="100" />';
      var imgs = '';
      if(num == 0){
        return '';
    }else{
        for(var i=0; i<num; i++){
            imgs += img;
        }
    }
      return imgs;
}

可以看看img标签的onerror事件,

<img src="..." onerror="javascript:this.src='...';$(this).parent().attr('href','...');"/>

非专业前端,非最佳实践,仅参考。

$('.uu').each(function(){
    for(var i = 0; i < 4 - $(this).find('img').length; i++){
        $(this).append('<img>');
    }
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题