Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery在包含img URL的数组中循环并附加到div_Jquery_Html_Arrays_Append - Fatal编程技术网

使用jQuery在包含img URL的数组中循环并附加到div

使用jQuery在包含img URL的数组中循环并附加到div,jquery,html,arrays,append,Jquery,Html,Arrays,Append,我试图使用jQuery将一系列图像中的单个图像附加到div的底部,div的数量理论上是无限的,因此图像必须重复 为此,我尝试创建一个带有图像URL列表的数组,然后将img附加到带有类.entry的divs中,其中src=“”是从数组中获取的图像 我的代码: function mascot_aside(){ $('.entry')。每个(函数(i){ var吉祥物=[ "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chi

我试图使用jQuery将一系列图像中的单个图像附加到div的底部,div的数量理论上是无限的,因此图像必须重复

为此,我尝试创建一个带有图像URL列表的
数组,然后将
img
附加到带有类
.entry
的divs中,其中
src=“”
是从数组中获取的图像

我的代码:

function mascot_aside(){
$('.entry')。每个(函数(i){
var吉祥物=[
"http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chisame.gif",
"http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chachamaru.gif",
"http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_eva.gif",
];
$(此)。附加(“”);
}); 
}
吉祥物放在一边();
但是,我已经看到,答案代码的问题是,如果div多于图像,则会加载“未定义”的图像


不管怎么说,这些图片似乎都没有出现,我也不知道我在做什么,所以任何帮助都将不胜感激。谢谢

刚刚在代码中发现了两个错误

  • 在迭代之外声明数组。(不是问题,但不应该是这样)
  • 存在值串联问题
  • 试试看

    功能吉祥物{
    var吉祥物=[
    "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chisame.gif",
    "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chachamaru.gif",
    "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_eva.gif", ];
    $('.entry')。每个(函数(i){
    $(此)。附加(“”);
    });
    }
    

    难道你没有因为忘记使用
    +
    连接操作符而从代码中得到语法错误吗?我假设你知道当你的Javascript不工作时,首先要检查的地方是Javascript控制台。至少75%的时候你会在那里看到一个错误,我认为这个链接会对你有所帮助。[链接]()我使用记事本和谷歌浏览器,我只是涉猎一下。对不起谢谢你。我对这一点很不熟悉,所以我甚至没有注意到。@Jenny很乐意帮助。。!
    function mascot_aside(){        
        $('.entry').each(function(i){
        var mascots = [
    "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chisame.gif",
    "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chachamaru.gif",
    "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_eva.gif",
        ];
    
        $(this).append('<img src='mascots[i % mascots.length]' class="mascot" />'); 
        }); 
    }
    
    mascot_aside();
    
    function mascot_aside() {
        var mascots = [
            "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chisame.gif",
            "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_chachamaru.gif",
            "http://i454.photobucket.com/albums/qq268/Princess_Kally/negi_eva.gif", ];
    
        $('.entry').each(function (i) {
    
            $(this).append('<img src=' + mascots[i % mascots.length] + ' class="mascot" />');
        });
    }