Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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 将数组输出为嵌套的div_Jquery_Arrays_Json - Fatal编程技术网

Jquery 将数组输出为嵌套的div

Jquery 将数组输出为嵌套的div,jquery,arrays,json,Jquery,Arrays,Json,我有一个数组,我想输出为嵌套的div。它有一点bug,但我无法找出HTML问题在哪里 我希望数据输出为: <div id="container"> <div class="item">Data comes out here</div> <div class="item">Data comes out here</div> <div class="item">Data comes out here</d

我有一个数组,我想输出为嵌套的div。它有一点bug,但我无法找出HTML问题在哪里

我希望数据输出为:

<div id="container">
   <div class="item">Data comes out here</div>
   <div class="item">Data comes out here</div>
   <div class="item">Data comes out here</div>
我无法确定如何将数组输出调整为在第一个
div中不输出,而是在
容器
div中输出
div

代码:

jQuery(函数(){
$.ajax({
网址:'http://www.sagittarius-digital.com/news.rss“,/*来源*/
数据类型:“xml”
}).done(函数(xml){
var items=$(xml).find('item').map(函数(){
变量$item=$(此项);
var数组=“”;
数组+='';
数组+='';
返回数组;
}).get();
$('div.item').append(items.join('');
}).fail(函数(){
console.log('error',参数)
})
})

使用.after代替.append,如下所示:

<div id="container">
    <div class="item">
       <div class="item">Data comes out here</div>
       <div class="item">Data comes out here</div>
       <div class="item">Data comes out here</div>
    </div>
</div>
$('div.item').after(items.join(' '));
而不是
$('div.item')。追加(items.join(“”))使用

 $('#container').append(items.join(' '));

因为它将附加一个额外的
类划分。

我猜这一行会导致当前行为:

$('div.item').append(items.join(' '));
尝试更改为:

$('div.item').after(items.join(' '));
它不会插入到div中,而是直接插入到。

$(xml).find('item')。each(函数(索引,元素)
$(xml).find('item').each(function(index,element)
{
    var item = $('<div class="item"></div>');
    var link = item.append('<a href="'+$(element).find("link").text()+'"></a>')
    link.append("<h2>"+ $(element).find("title").text() + "</h2>");
    link.append("<p>"+ $(element).find("description").text() + "</p>");

    //Suggestion: use moment.js :P
    var date     = new Date( $(element).find("pubDate").text() );
    var yyyymmdd = date.getFullYear() +''+ (date.getMonth()+1) +''+ date.getDate();

    link.append("<p>"+yyyymmdd+"</p>");
    link.append("<p>Category: "+$(element).find("category").text()+"</p>");

    item.appendTo("#container")
});
{ var项目=$(''); var link=item.append(“”) link.append(“+$(元素).find(“title”).text()+”; link.append(“”+$(元素).find(“description”).text()+”

”; //建议:使用moment.js:P 变量日期=新日期($(元素).find(“pubDate”).text(); var yyyymmdd=date.getFullYear()++(date.getMonth()+1)+''+date.getDate(); link.append(“”+yyyymmdd+“

”); link.append(“Category:”+$(元素).find(“Category”).text()+“

”; 项目.附于(“#容器”) });
更好的方法,应该是:)

$(xml).find('item').each(function(index,element)
{
    var item = $('<div class="item"></div>');
    var link = item.append('<a href="'+$(element).find("link").text()+'"></a>')
    link.append("<h2>"+ $(element).find("title").text() + "</h2>");
    link.append("<p>"+ $(element).find("description").text() + "</p>");

    //Suggestion: use moment.js :P
    var date     = new Date( $(element).find("pubDate").text() );
    var yyyymmdd = date.getFullYear() +''+ (date.getMonth()+1) +''+ date.getDate();

    link.append("<p>"+yyyymmdd+"</p>");
    link.append("<p>Category: "+$(element).find("category").text()+"</p>");

    item.appendTo("#container")
});