"What does mean?"
P粉358281574
P粉358281574 2023-08-17 10:48:39
0
2
945
<p>I'm trying to get the value returned from a function <code>alert</code> and I get the following in the popup: </p> <pre class="brush:none;toolbar:false;">[object Object] </pre> <p>The following is the JavaScript code: </p> <pre class="brush:html;toolbar:false;"><script type="text/javascript"> $(function () { var $main = $('#main'), $1 = $('#1'), $2 = $('#2'); $2.hide(); // Hide div#2 when the page loads $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> </pre> <p><code>whichIsVisible</code> is the function I'm trying to check. </p>
P粉358281574
P粉358281574

reply all(2)
P粉458913655

As others have pointed out, this is the default serialization of an object. But why is it [object Object] and not just [object]?

This is because there are different types of objects in Javascript!

  • Function Object:
    stringify(function (){}) -> [object Function]
  • Array object
    stringify([]) -> [object Array]
  • Regular Expression Object:
    stringify(/x/) -> [object RegExp]
  • Date Object:
    stringify(new Date) -> [object Date]
  • there are more
  • AlsoObject object!
    stringify({}) -> [object Object]

This is because the constructor is called Object (capital "O"), and the term "object" (lowercase "o") refers to the structural nature of the object.

Usually, when talking about "objects" in Javascript, you actually mean "Object objects", not other types.

Where stringify should be like this:

function stringify (x) {
    console.log(Object.prototype.toString.call(x));
}

P粉465287592

The default result of converting an object to a string is "[object Object]".

Since you are dealing with jQuery objects, you may want to do the following

alert(whichIsVisible()[0].id);

To print the ID of the element.

As mentioned in the comments, you should use the tools included in browsers like Firefox or Chrome to inspect objects, instead of using alert, you can do console.log(whichIsVisible( )).

Note: ID should not start with a number.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!