最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery each returns [object Object] - Stack Overflow

programmeradmin6浏览0评论

My problem is that the html variable returns something like this: [object Object][object Object][object Object][object Object][object Object], instead of the elements.

What should i do different?

var html = '';
$.each(data.response, function(index, value) { 
    var tr = $('<tr>');
    var tr_data = '<td>asd</td>';
    html += tr.data('trackinfo',value).html(tr_data);   
});

$(target).html(html);

My problem is that the html variable returns something like this: [object Object][object Object][object Object][object Object][object Object], instead of the elements.

What should i do different?

var html = '';
$.each(data.response, function(index, value) { 
    var tr = $('<tr>');
    var tr_data = '<td>asd</td>';
    html += tr.data('trackinfo',value).html(tr_data);   
});

$(target).html(html);
Share Improve this question edited Dec 22, 2015 at 20:08 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 13, 2012 at 16:11 passatgtpassatgt 4,4425 gold badges43 silver badges56 bronze badges 1
  • Can you please post what the data variable contains. I'm guessing JSON? – Rory McCrossan Commented Feb 13, 2012 at 16:13
Add a ment  | 

2 Answers 2

Reset to default 4

That's because you're setting the data on the tr and then filling it with your html, but still concatinating an object, which converts it to a string... aka

"[object Object]"

Not exactly sure what you're after but you might try changing this...

html += tr.data('trackinfo',value).html(tr_data);   

To this...

html += tr.data('trackinfo',value).html(tr_data).html();   

By default, Jquery creates objects not html mark-up. To get html you should to call html() method.

Here is working code:

var html = '';
$.each(data.response, function(index, value) { 
    var tr = $('<tr>');
    var tr_data = '<td>asd</td>';
    html += tr.data('trackinfo',value).html(tr_data);   
});

$(target).html(html);
发布评论

评论列表(0)

  1. 暂无评论