How to inspect Javascript Objects

后端 未结 8 552
半阙折子戏
半阙折子戏 2020-12-07 10:31

How can I inspect an Object in an alert box? Normally alerting an Object just throws the nodename:

alert(document);

But I want to get the p

8条回答
  •  鱼传尺愫
    2020-12-07 10:42

    Use your console:

    console.log(object);
    

    Or if you are inspecting html dom elements use console.dir(object). Example:

    let element = document.getElementById('alertBoxContainer');
    console.dir(element);
    

    Or if you have an array of js objects you could use:

    console.table(objectArr);
    

    If you are outputting a lot of console.log(objects) you can also write

    console.log({ objectName1 });
    console.log({ objectName2 });
    

    This will help you label the objects written to console.

提交回复
热议问题