问题描述
我正在尝试提醒函数返回的值,我在警告中得到此信息
I am trying to alert a returned value from a function and i get this in the alert
[object Object]
[object Object]
这里是javascript代码
here is the javascript code
<script type="text/javascript">
$(function ()
{
var $main = $('#main'),
$1 = $('#1'),
$2 = $('#2');
$2.hide(); // hide div#2 when the page is loaded
$main.click(function ()
{
$1.toggle();
$2.toggle();
});
$('#senddvd').click(function ()
{
alert('hello');
var a=whichIsVisible();
alert(whichIsVisible());
});
function whichIsVisible()
{
if (!$1.is(':hidden')) return $1;
if (!$2.is(':hidden')) return $2;
}
});
</script>
whichIsVisible是我试图检查的功能
whichIsVisible is the function which i am trying to check on
推荐答案
从对象到字符串的默认转换是[object Object]
。
The default conversion from an object to string is "[object Object]"
.
当您处理jQuery对象时,您可能想要
As you are dealing with jQuery objects, you might want to do
alert(whichIsVisible()[0].id);
打印元素的ID。
正如评论中所提到的,您应该使用Firefox或Chrome等浏览器中的工具来通过 console.log(whichIsVisible())
而不是<$ c $来内省对象。 c> alert 。
As mentioned in the comments, you should the use tools included in browsers like Firefox or Chrome to introspect objects by doing console.log(whichIsVisible())
instead of alert
.
Sidenote :ID不应以数字开头。
Sidenote: IDs should not start with digits.
这篇关于[object Object]是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!