简体   繁体   English

jQuery各自返回[object Object]

[英]jQuery each returns [object Object]

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. 我的问题是html变量返回的内容如下:[object Object] [object Object] [object Object] [object Object] [object Object],而不是元素。

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);

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 那是因为您要在tr上设置数据,然后用html填充它,但是仍然隐含一个对象,将其转换为字符串。

"[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. 默认情况下,jQuery创建的对象不是html标记。 To get html you should to call html() method. 要获取html,您应该调用html()方法。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM