javascript - 关于一个获取数据问题
伊谢尔伦
伊谢尔伦 2017-04-11 10:22:22
[JavaScript讨论组]

补全图片

<!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>
    
    <p class="animationbox">
        <p 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"/>
        </p>
        <p 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"/>
        </p>
    </p>
    <script src="http://cdn.bootcss.com/jquery/2.2.1/jquery.js"></script>
    <script>

        $(function() {
            
        });
                
    </script>
      
</body>
</html>
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(4)
巴扎黑
 $(function() {
       var uus =  $("p.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($('p.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;
}
PHPz

可以看看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>');
    }
})
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号