[英]printing javascript object in HTML
I am trying to print a javascript
object whose content i don't know in HTML
我正在尝试打印一个我在
HTML
不知道其内容的javascript
对象
$(".side div .data").replaceWith('<p class="data">' + $.each(d, function (key, value) {
return key;
}) + '</p>');
I want to loop through the object and print their key-value
pairs 我想遍历对象并打印其
key-value
对
if the Object does not contain looping references you can use 如果对象不包含循环引用,则可以使用
JSON.stringify(object)
Otherwise you can try for in loop 否则你可以尝试循环
for(var i in obj) {
console.log(i + " : " + obj[i]);
}
want to loop through the object and print their key-value pairs
想要遍历对象并打印其键值对
Try using $.map()
, Array.prototype.join()
尝试使用
$.map()
, Array.prototype.join()
var d = {a:1, b:2, c:3}; $(".side div .data").replaceWith('<p class="data">' + $.map(d, function (value, key) { return key + ":" + value; }).join(" ") + '</p>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="side"> <div> <span class="data"></span> </div> </div>
if you don't want date to be print in any specific formate then silmply use $(".data").append(JSON.stringify(d));
如果您不希望日期以任何特定格式打印,则使用
$(".data").append(JSON.stringify(d));
otherwise try the following 否则尝试以下
var d = { a: 1, b: 2, c: 3 }; $.each(d, function(i, item) { $(".data").append(i + ' -> ' + item + '<br> '); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="side"> <div> <span class="data"></span> </div> </div>
From your mark up in the code you provided, it looks like you are trying to do... 从您提供的代码中的标记来看,您似乎正在尝试...
$(".side div .data").html(JSON.stringify(d))
or even just.... as the stuff you are returning may already be in format html 甚至只是...。因为您返回的内容可能已经是html格式
$(".side div .data").html(d)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.