admin管理员组

文章数量:1401241

I am learning JS and after fiddling around with adding elements etc I tried to do an alert() with the object but instead got this error: [object htmltableelement] so I then tried:

alert(t.toString());

and got the same error... how can I see the contents of the object?

I am learning JS and after fiddling around with adding elements etc I tried to do an alert() with the object but instead got this error: [object htmltableelement] so I then tried:

alert(t.toString());

and got the same error... how can I see the contents of the object?

Share Improve this question edited Jul 29, 2011 at 3:56 Lightness Races in Orbit 386k77 gold badges666 silver badges1.1k bronze badges asked Jul 29, 2011 at 3:52 RyanRyan 10.1k23 gold badges68 silver badges103 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

you can use firebug:

console.log(t);

or you can use innerHTML;

alert(t.innerHTML);

The way I normally do this is by using FireBug firefox add-on. Add a break point in your JavaScript then you can view any object and all its keys/values.

See everything:

for(var key in t)
  alert('key:' + key + ', value: ' + t[key]);

You may want to replace alert with console to avoid 100s of alerts

function domObjectToString(domObject){   
    if(typeof(domObject) ==='object'){    
        var divElement = document.createElement('div') ;    
        divElement.appendChild(domObject);    
        return divElement.innerHTML;    
    }else{     
        return domObject.toString();     
    }
}

---- steps follow -----
1. check the domObject Type [object]
2. If Object than
a. Create an "Div" element
b. append DomObject To It
c. get The innerHTML of the "div" it Gives The String
3. If Not an Object than convert to String and return it .

That is not an error. That is the default string representation of an object.

Either iterate through the object's properties and output them one by one, or use a proper debugging tool like Firebug that'll give you the ability to really examine your variables.

本文标签: javascriptViewing an objectStack Overflow