[对象对象] 是什么意思? [英] What does [object Object] mean?

查看:44
本文介绍了[对象对象] 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提醒一个函数的返回值,我在提醒中得到了这个:

I am trying to alert a returned value from a function and I get this in the alert:

[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 是我要检查的函数.

推荐答案

对象到字符串的默认转换是"[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()) 而不是 alert.

As mentioned in the comments, you should use the tools included in browsers like Firefox or Chrome to introspect objects by doing console.log(whichIsVisible()) instead of alert.

旁注:ID 不应以数字开头.

Sidenote: IDs should not start with digits.

这篇关于[对象对象] 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆