如何用动态填充div?

时间:2015-01-30 18:11:46

标签: javascript jquery html flickr

http://jsfiddle.net/84nv2dmL/

我正在尝试按顺序显示这些字母图像。我尝试动态创建div并用img填充它们,但这不起作用。如何才能按顺序显示这些字母?


jsfiddle代码:

function getQueryStringVar(name){
    var qs = window.location.search.slice(1);
    var props = qs.split("&");
    for (var i=0 ; i < props.length;i++){
        var pair = props[i].split("=");
        if(pair[0] === name) {
            return decodeURIComponent(pair[1]);
        }
    }
}

function getLetterImage(tag){

var flickerAPI = "https://www.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";

            return $.getJSON( flickerAPI, {
                tags: tag,
                tagmode: "all",
                format: "json"
            })
            .then(function (flickrdata) {
                //console.log(flickrdata);
                var i = Math.floor(Math.random() * flickrdata.items.length);
                var item = flickrdata.items[i];
                var url = item.media.m;
                return url;
                }); 
}



$(document).ready(function() {
        var name = getQueryStringVar("name") || "Derek";

            var str = "letter,";
            var searchtags = new Array()
            for (var i = 0; i < name.length; i++) {
                //console.log(str.concat(searchtags.charAt(i)));
                searchtags[i] = str.concat(name.charAt(i));
            }
            for (var j = 0; j < name.length; j++){
            var request = getLetterImage(searchtags[j]);
            request.done(function(url) {
                $("body").append("<img src="+ url + "></img>");

                //var ele = document.createElement("div");
                //ele.setAttribute("class", "img" + j--);
                //document.body.appendChild(ele);

                //$("<img src="+ url +"></img>").appendTo("img"+j);

            });
            }
            //$("#img"+i).html("<img src="+ url + "></img>");

});

2 个答案:

答案 0 :(得分:0)

$("#div_id").append("<img src="+ url + "></img>");

此处div_id是您为div提供的ID。 追加将img标记与源src添加到其中。

请提供html代码以及脚本以帮助更好地理解问题

答案 1 :(得分:0)

您基本上需要跟踪将图像附加到DOM的顺序,并确保它们与名称中的字母同步。修复了一个小提琴。评论符合:

http://jsfiddle.net/84nv2dmL/2/

function getQueryStringVar(name) {
    var qs = window.location.search.slice(1);
    var props = qs.split("&");
    for (var i = 0; i < props.length; i++) {
        var pair = props[i].split("=");
        if (pair[0] === name) {
            return decodeURIComponent(pair[1]);
        }
    }
}

function getLetterImage(tag) {

    var flickerAPI = "https://www.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";

    return $.getJSON(flickerAPI, {
            tags: tag.char,
            tagmode: "all",
            format: "json"
        })
        .then(function(flickrdata) {
            //console.log(flickrdata);
            var i = Math.floor(Math.random() * flickrdata.items.length);
            var item = flickrdata.items[i];
            var url = item.media.m;
            // return an object with url AND index
            return {
                ind: tag.ind,
                img: url
            };
        });
}



$(document).ready(function() {
    var name = getQueryStringVar("name") || "Derek";
    var urls = new Array(name.length); // array or URLs, in correct order
    var urlCounter = []; // keeps count or URLs received
    var str = "letter,";
    var searchtags = new Array();
    for (var i = 0; i < name.length; i++) {
        searchtags[i] = {
            char: str.concat(name.charAt(i)),
            ind: i
        };
    }
    for (var j = 0; j < name.length; j++) {
        var request = getLetterImage(searchtags[j]);
        request.done(function(url) {
            // when request is done, place image url in 'urls' array, in the correct order
            urls[url.ind] = url.img;

            // push object to the counter array, just to keep count
            urlCounter.push(url);

            // check if all image urls have been collected
            checkComplete();
        });

    }

    function checkComplete() {

        // if the number of URLs received is equal
        // to the number of characters in the name
        // append the images from the ordered array
        if (urlCounter.length == name.length) {
            for (var k = 0; k < urls.length; k++) {
                $("body").append("<img src=" + urls[k] + "></img>");
            }
        }
    }

});