I'm attempting
var a = $("<a/>");
then I'm trying to fill this empty variable with the contents of an object, set to variable 'obj'
obj = {
Name: " Ben",
number: 666,
}
I have tried to append a using the various methods
a.append(obj);
a.append("("+obj+")");
but if I then do:
console.log(a)
there is no way to tell if the object is now in the link tag. If I look in the console on the site I can see
([object Object])
rather than the contents. Am I missing something?
答案 0 :(得分:3)
只需附加对象,它就会被隐式强制转换为字符串,因此您会看到[Object object]
。
您需要手动将对象转换为易读字符串。您可以使用JSON.stringify()
来完成此操作,如下所示:
var $a = $('<a href="#"/>').appendTo('body');
var obj = {
Name: " Ben",
number: 666
};
$a.text(JSON.stringify(obj));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 1 :(得分:0)
你必须使用对象的属性 如下所示
var a = $("<a/>");
obj = {
Name: " Ben",
number: 666,
}
a.text(obj.Name + ' ' + obj.number);