Javascript 如何提醒javascript对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3580754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to alert javascript object
提问by mapet
I am trying to study the jquery class, but I have a hard time debugging an object because I can't see the element inside of it
我正在尝试研究 jquery 类,但是我很难调试对象,因为我看不到其中的元素
$("#birds").autocomplete({
source: "search.php",
select: function(event, ui) {
alert(ui);
}
});
it returns [object Object].. :( My question is how can I alert the object so that I can see the element?
它返回 [object Object] .. :( 我的问题是如何提醒对象以便我可以看到元素?
回答by Scott Evernden
i recommend you use FireBug for debugging javascript. then you can just do
我建议您使用FireBug 来调试 javascript。那么你可以做
console.log(ui)
and it'll log the object in a form you can expand
它将以您可以展开的形式记录对象
回答by Shafin Mahmud
Just convert your object to a JSON object using stringfy.
alert(JSON.stringify(yourObjectVariable));
只需使用 stringfy 将您的对象转换为 JSON 对象。
alert(JSON.stringify(yourObjectVariable));
simple as pie :)
简单的馅饼:)
回答by Neeraj Singh
you can also try Java Script method:
您也可以尝试 Java Script 方法:
// Alert javascript object in alert box
function alertObject(obj){
for(var key in obj) {
alert('key: ' + key + '\n' + 'value: ' + obj[key]);
if( typeof obj[key] === 'object' ) {
alertObject(obj[key]);
}
}
}
Here 'obj' is:
这里的'obj'是:
// your object var
var getObject = {};
// object set with key an val
getObject.swfVersionStr = '10.0';
getObject.xiSwfUrlStr = null;
getObject.flashvarsObj = {};
getObject.parObj = {allowfullscreen: "true",wmode: "window",menu: "false"};
Call like this:
像这样调用:
alertObject(getObject );
So, simple.. :)
很简单.. :)
回答by thecodedeveloper.com
If you are using Firefox then you can alert object value like below code
如果您使用的是 Firefox,那么您可以像下面的代码一样提醒对象值
alert(object.toSource()); // for you alert(ul.toSource());
That above code worked fine for me.
上面的代码对我来说很好用。
回答by Rock Dial
Convert your array or objectto a JSON objectusing stringify.
将您的数组或对象的JSON对象使用stringify。
Example:
例子:
var obj = { "name":"bayiha", "age":30, "city":"Eseka"};
var myJSON = JSON.stringify(obj);
alert(myJSON);

