我需要从带有 JSON 对象的数组创建一个图像库

我有一个家庭作业的任务,我需要创建一个显示 4 个图像的简单画廊。


我需要将所有图像放入 Array 中,其中每张照片都将位于 JSON 对象中并从数组加载图像。这就是我现在的位置:


<body>

 <div class="images">


</div>


(function (){

    let array_img = [

        {

            filename:"20140222_131314",

            title:"img1",        


        },

        {

            filename:"20140712_203709",

            title:"img2",


        },

        {

            filename:"20190318_182928",

            title:"img3"

        },

        {

            filename:"20190422_181219",

            title:"img4"

        }

    ]

    for (var i = 0 ; i<array_img.length ; i++){

        arr_img = array_img[i]

        var container = $(".images");

        $container.append("<img/>").attr("src=/Photos/20140222_131314.jpg")


    }


}());

或者我应该创建一个额外的变量 img= array_img[i] 并从中创建一个文本节点,然后附加 . 但是如何附加和设置所有图像的 src 路径。也许我需要为对象结构创建和清空“src”?但我错过了一些东西。有人能帮我吗?


慕斯709654
浏览 125回答 2
2回答

慕侠2389804

这应该运行。确保在变量之前有一个 $。(function() {&nbsp; let array_img = [{&nbsp; &nbsp; &nbsp; filename: "20140222_131314",&nbsp; &nbsp; &nbsp; title: "img1",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; filename: "20140712_203709",&nbsp; &nbsp; &nbsp; title: "img2",&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; filename: "20190318_182928",&nbsp; &nbsp; &nbsp; title: "img3"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; filename: "20190422_181219",&nbsp; &nbsp; &nbsp; title: "img4"&nbsp; &nbsp; }&nbsp; ]&nbsp; for (var i = 0; i < array_img.length; i++) {&nbsp; &nbsp; $arr_img = array_img[i]&nbsp; &nbsp; var $container = $(".images");&nbsp; &nbsp; $container.append("<img src=/Photos/"+$arr_img[0]+".jpg/>");&nbsp; }}());<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="images"></div>

开心每一天1111

我这样做了,现在它起作用了。我不知道这是否是最好的方法,但是...(function (){&nbsp; &nbsp; let array_img = [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename:"20140222_131314.jpg",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title:"img1",&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename:"20140712_203709.jpg",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title:"img2",&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename:"20190318_182928.jpg",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title:"img3"&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filename:"20190422_181219.jpg",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title:"img4"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]&nbsp; &nbsp; for (var i = 0 ; i<array_img.length ; i++){&nbsp; &nbsp; &nbsp; &nbsp; var arr_img = array_img = [i]&nbsp; &nbsp; &nbsp; &nbsp; var $container = $(".images");&nbsp; &nbsp; &nbsp; &nbsp; $container.append(arr_img[i+1],"<img src=photos/20140222_131314.jpg>")&nbsp; &nbsp; &nbsp; &nbsp; $container.append(arr_img[i+1],"<img src=photos/20140712_203709.jpg>")&nbsp; &nbsp; &nbsp; &nbsp; $container.append(arr_img[i+1],"<img src=photos/20190318_182928.jpg>")&nbsp; &nbsp; &nbsp; &nbsp; $container.append(arr_img[i+1],"<img src=photos/20190422_181219.jpg>")&nbsp; &nbsp; }}());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript