-1
jQuery.each(input, function(key, value){
    jQuery('<div/>', {
        class: 'info',
        id: value.id,
        text: jQuery('<div/>', {class: 'image', text: '<img src="'+value.image+'">'})+jQuery('<div/>', {class: 'text_data', text: value.name})
    }).appendTo('#list');
});

是否有可能以某种方式将对象转换为text属性中的字符串(应该没有循环)。如果需要,可以用函数包装。

更新:

<div class="info" id="29">[object Object][object Object]</div>
<div class="info" id="30">[object Object][object Object]</div>
<div class="info" id="31">[object Object][object Object]</div>

所以我需要更新这一行jQuery('<div/>', {class: 'image', text: '<img src="'+value.image+'">'})+jQuery('<div/>', {class: 'text_data', text: value.name}),以便有一个字符串。

4

3 回答 3

1

尝试

 $('<div>').append($('#item-of-interest').clone()).html();
于 2013-01-22T20:47:59.300 回答
1

你可以使用JSON.stringify,它序列化一个对象。大多数现代浏览器都天真地支持这种方法,但对于那些不支持的浏览器,您可以包含一个 JS 版本:

var obj = {
  name: 'myObj'
};

JSON.stringify(obj);
于 2013-01-22T20:49:32.137 回答
1

我将扩展雷切尔的答案

您在这里创建两个元素

jQuery('<div/>', {class: 'image', text: '<img src="'+value.image+'">'})+jQuery('<div/>', {class: 'text_data', text: value.name})

要将其作为字符串获取.. 创建一个容器元素并附加这两个元素.. 这样您就可以通过调用 .html() 将其作为 html 字符串返回

$('<div/>') // create a div container
   .append(jQuery('<div/>', {class: 'image', text: '<img src="'+value.image+'">'})) // append objects
   .append(jQuery('<div/>', {class: 'text_data', text: value.name}))
   .html() // <-- now you have it as a string 
于 2013-01-22T20:57:34.523 回答