Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Javascript For循环中的For循环不';t应用于循环的正确迭代?_Javascript_Loops_Tumblr - Fatal编程技术网

Javascript For循环中的For循环不';t应用于循环的正确迭代?

Javascript For循环中的For循环不';t应用于循环的正确迭代?,javascript,loops,tumblr,Javascript,Loops,Tumblr,我有一个脚本,可以提取Tumblr提要,然后输出5篇最新文章。我遇到的问题是标记数组的循环。循环可以工作,但它将标记应用于post循环的第一次迭代,而不是应用于标记实际所属的迭代。有没有办法把贴子标签“锁定”到正确的贴子上 post循环通过检查body_abstract来确定文本类型post是否具有“更多阅读”链接。然后它构造了post。然后它应该把标签放在它们所属的地方。在返回中,我当前有五个带有标签的帖子中的一个,这是第三个帖子,但是标签一直被修改到第一个帖子。想法 for(i=0;

我有一个脚本,可以提取Tumblr提要,然后输出5篇最新文章。我遇到的问题是标记数组的循环。循环可以工作,但它将标记应用于post循环的第一次迭代,而不是应用于标记实际所属的迭代。有没有办法把贴子标签“锁定”到正确的贴子上

post循环通过检查body_abstract来确定文本类型post是否具有“更多阅读”链接。然后它构造了post。然后它应该把标签放在它们所属的地方。在返回中,我当前有五个带有标签的帖子中的一个,这是第三个帖子,但是标签一直被修改到第一个帖子。想法

    for(i=0; (i < data.response.total_posts) && (i < 5); i++){
            if (data.response.posts[i].type == "text"){
                if (data.response.posts[i].hasOwnProperty('body_abstract')){
                    $('article').append(
                        '<div class="blogtitle">'
                            + '<a href="' + data.response.posts[i].short_url + '">'
                                + '<h2>' + data.response.posts[i].title + '</h2>'
                            + '</a>'
                        + '</div>'
                        + '<div class="row">'
                            + '<div class="postedby col-sm-6 col-md-6">'
                                + '<img class="avatar pull-left" src="http://api.tumblr.com/v2/blog/' + data.response.posts[i].post_author + '.tumblr.com/avatar" alt="Avatar" height="64" width="64" />'
                                + '<p>Posted By <br />' + data.response.posts[i].post_author + ' <br />'
                                + '<span class="glyphicon glyphicon-time"></span>'+ data.response.posts[i].date + '</p>'
                            + '</div>'
                            + '<div class="col-sm-6 col-md-6" id="tags">'
                                + '<span class="glyphicon glyphicon-bookmark"></span>'
                            + '</div>'
                        + '</div>');
                    if (data.response.posts[i].tags.length == 0){
                        $('#tags').append('<p>No Tags</p>');
                    }
                    else {
                        for (j=0; j < data.response.posts[i].tags.length; j++){
                            var dashedTag = data.response.posts[i].tags[j].replace(/ /g,"-");
                            tagLinks.push(dashedTag);
                            $('#tags').append(
                                '<a href="http://www.nevermorestudiosonline.com/tagsearch.php?' + tagLinks[j] + '">#' + data.response.posts[i].tags[j] + '</a>&nbsp;'
                            );
                        };
                    };
                }
                else {
                    $('article').append(
                        '<div class="blogtitle">'
                            + '<a href="' + data.response.posts[i].short_url + '">'
                                + '<h2>' + data.response.posts[i].title + '</h2>'
                            + '</a>'
                        + '</div>'
                        + '<div class="row">'
                            + '<div class="postedby col-sm-6 col-md-6">'
                                + '<img class="avatar pull-left" src="http://api.tumblr.com/v2/blog/' + data.response.posts[i].post_author + '.tumblr.com/avatar" alt="Avatar" height="64" width="64" />'
                                + '<p>Posted By <br />' + data.response.posts[i].post_author + ' <br />'
                                + '<span class="glyphicon glyphicon-time"></span>'+ data.response.posts[i].date + '</p>'
                            + '</div>'
                            + '<div class="col-sm-6 col-md-6" id="tags">'
                                + '<span class="glyphicon glyphicon-bookmark"></span>'
                            + '</div>'
                        + '</div>');
                    if (data.response.posts[i].tags.length == 0){
                        $('#tags').append('<p>No Tags</p>');
                    }
                    else {
                        for (j=0; j < data.response.posts[i].tags.length; j++){
                            var dashedTag = data.response.posts[i].tags[j].replace(/ /g,"-");
                            tagLinks.push(dashedTag);
                            $('#tags').append(
                                '<a href="http://www.nevermorestudiosonline.com/tagsearch.php?' + tagLinks[j] + '">#' + data.response.posts[i].tags[j] + '</a>&nbsp;'
                            );
                        };
                    };
                };
for(i=0;(i”+data.response.posts[i]。post_author+“
” +''+数据.response.posts[i].日期+'

' + '' + '' + '' + '' + ''); if(data.response.posts[i].tags.length==0){ $(“#标记”)。追加(“无标记”); } 否则{ 对于(j=0;j”+data.response.posts[i]。post_author+“
” +''+数据.response.posts[i].日期+'

' + '' + '' + '' + '' + ''); if(data.response.posts[i].tags.length==0){ $(“#标记”)。追加(“无标记”); } 否则{ 对于(j=0;j
$('article')
将创建一个jQuery对象,该对象围绕页面上的所有
元素。如果调用
append
并传入标记(字符串),则该标记定义的元素将附加到这些元素的每个。也就是说,如果我有:

<div></div>
<div></div>
<div></div>

…然后执行
$(“div”)。附加(“Hi”)
,我将以以下内容结束:

<div><span>Hi</span></div>
<div><span>Hi</span></div>
<div><span>Hi</span></div>
Hi
你好
你好
如果您想针对特定元素,可以使用
.eq(x)
:eq
仅匹配特定元素。例如,
$('article')。eq(0)
为您提供一个jQuery对象,其中只包含第一篇
文章,就像
$('article:eq(0)
一样



旁注:您没有将
放在块之后,例如附加到
if
else
for
等的块。

如果您有错误,您需要以下一种方式在for循环外部声明变量“tagLinks”:

var tagLinks = [];    
for (j=0; j < data.response.posts[i].tags.length; j++){
    var dashedTag = data.response.posts[i].tags[j].replace(/ /g,"-");
    tagLinks.push(dashedTag);
    $('#tags').append('<a href="http://www.nevermorestudiosonline.com/tagsearch.php?' + tagLinks[j] + '">#' + data.response.posts[i].tags[j] + '</a>&nbsp;');
};
var tagLinks=[];
对于(j=0;j
这种行为是因为使用ID选择器的$(“#标记”)只返回相同ID的第一个元素 因此,您需要做的是为每个tags DIV指定一个特定的ID,并使用ID选择器以正确的DIV元素(您的tags容器)为目标

for(i=0;(i”+data.response.posts[i]。post_author+“
” +''+数据.response.posts[i].日期+'

' + '' + '' + '' + '' + ''); if(data.response.posts[i].tags.length==0){ $('#tags'+i).append('No tags

'); } 否则{ 对于(j=0;j for(i=0; (i < data.response.total_posts) && (i < 5); i++){ if (data.response.posts[i].type == "text"){ if (data.response.posts[i].hasOwnProperty('body_abstract')){ $('article').append( '<div class="blogtitle">' + '<a href="' + data.response.posts[i].short_url + '">' + '<h2>' + data.response.posts[i].title + '</h2>' + '</a>' + '</div>' + '<div class="row">' + '<div class="postedby col-sm-6 col-md-6">' + '<img class="avatar pull-left" src="http://api.tumblr.com/v2/blog/' + data.response.posts[i].post_author + '.tumblr.com/avatar" alt="Avatar" height="64" width="64" />' + '<p>Posted By <br />' + data.response.posts[i].post_author + ' <br />' + '<span class="glyphicon glyphicon-time"></span>'+ data.response.posts[i].date + '</p>' + '</div>' + '<div class="col-sm-6 col-md-6" id="tags_"'+i+'>' + '<span class="glyphicon glyphicon-bookmark"></span>' + '</div>' + '</div>'); if (data.response.posts[i].tags.length == 0){ $('#tags_'+i).append('<p>No Tags</p>'); } else { for (j=0; j < data.response.posts[i].tags.length; j++){ var dashedTag = data.response.posts[i].tags[j].replace(/ /g,"-"); tagLinks.push(dashedTag); $('#tags_'+i).append( '<a href="http://www.nevermorestudiosonline.com/tagsearch.php?' + tagLinks[j] + '">#' + data.response.posts[i].tags[j] + '</a>&nbsp;' ); }; }; } else { $('article').append( '<div class="blogtitle">' + '<a href="' + data.response.posts[i].short_url + '">' + '<h2>' + data.response.posts[i].title + '</h2>' + '</a>' + '</div>' + '<div class="row">' + '<div class="postedby col-sm-6 col-md-6">' + '<img class="avatar pull-left" src="http://api.tumblr.com/v2/blog/' + data.response.posts[i].post_author + '.tumblr.com/avatar" alt="Avatar" height="64" width="64" />' + '<p>Posted By <br />' + data.response.posts[i].post_author + ' <br />' + '<span class="glyphicon glyphicon-time"></span>'+ data.response.posts[i].date + '</p>' + '</div>' + '<div class="col-sm-6 col-md-6" id="tags_'+i+'">' + '<span class="glyphicon glyphicon-bookmark"></span>' + '</div>' + '</div>'); if (data.response.posts[i].tags.length == 0){ $('#tags_'+i).append('<p>No Tags</p>'); } else { for (j=0; j < data.response.posts[i].tags.length; j++){ var dashedTag = data.response.posts[i].tags[j].replace(/ /g,"-"); tagLinks.push(dashedTag); $('#tags_'+i).append( '<a href="http://www.nevermorestudiosonline.com/tagsearch.php?' + tagLinks[j] + '">#' + data.response.posts[i].tags[j] + '</a>&nbsp;' ); }; }; };