What does [object Object] mean?([对象对象] 是什么意思?)
问题描述
我试图提醒一个函数的返回值,我在提醒中得到了这个:
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.
这篇关于[对象对象] 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:[对象对象] 是什么意思?
				
        
 
            
        - 如何调试 CSS/Javascript 悬停问题 2022-01-01
 - 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
 - 如何向 ipc 渲染器发送添加回调 2022-01-01
 - 从原点悬停时触发 translateY() 2022-01-01
 - 如何显示带有换行符的文本标签? 2022-01-01
 - 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
 - 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
 - 为什么我的页面无法在 Github 上加载? 2022-01-01
 - 在不使用循环的情况下查找数字数组中的一项 2022-01-01
 - 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
 
						
						
						
						
						
				
				
				
				