Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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 Flickr API随机div背景_Jquery_Ajax_Json - Fatal编程技术网

Jquery Flickr API随机div背景

Jquery Flickr API随机div背景,jquery,ajax,json,Jquery,Ajax,Json,我想用FlickrAPI随机拍摄一张带有“风景”标签的照片。随机照片链接似乎是通过编程方式添加到ChromeDevTools中的html on inspect元素中的。但是没有照片。我做错了什么 $(document).ready(function() { $.getJSON("https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", {

我想用FlickrAPI随机拍摄一张带有“风景”标签的照片。随机照片链接似乎是通过编程方式添加到ChromeDevTools中的html on inspect元素中的。但是没有照片。我做错了什么

    $(document).ready(function() {


      $.getJSON("https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
               {
                  tags: "landscape",
                  format: "json"
                },

                //The callback function
                function(data) {

                  //Get random photo from the api's items array
                    var randomPhoto = data.items[Math.floor(Math.random() * data.items.length)];  


                    $(".portret").css({


                      position: "relative",
                      height: "100vh",
                    //Use the randomPhoto's link
                      backgroundImage: "url("+randomPhoto.link+")",
                      backgroundPosition: "center",
                      backgroundRepeat: "no-repeat",
                      backgroundSize: "cover"
                    });
                }

               );




    });

您必须使用
媒体
对象来设置背景图像

您使用的“链接”属性不正确,这是Flickr照片详细信息的链接。您只需要图像的url

试试看:

            $.getJSON("https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
               {
                  tags: "landscape",
                  format: "json"
                },

                //The callback function
                function(data) {

                  //Get random photo from the api's items array
                    var randomPhoto = data.items[Math.floor(Math.random() * data.items.length)];  


                    $(".portret").css({


                      position: "relative",
                      height: "100vh",
                    //Use the randomPhoto's link
                      backgroundImage: "url("+randomPhoto.media.m+")",
                      backgroundPosition: "center",
                      backgroundRepeat: "no-repeat",
                      backgroundSize: "cover"
                    });
                 }
             );