简体   繁体   English

如何在IE9上显示对象的内容

[英]How to show the content of object on IE9

var obj = {'test1':"value1", "test2": "value2" }
console.log(obj);

reuslt 重用

[object, Object]

Is is possible to show the content the object not to use JSON.stringify on IE9? 是否可以在IE9上显示不使用JSON.stringify的对象的内容?
like the following 像下面

{'test1':"value1", "test2": "value2" }

Try using console.dir() instead of console.log() - this will also work with other browsers' consoles. 尝试使用console.dir()而不是console.log() -这也可以与其他浏览器的控制台一起使用。

See also the MSDN article Internet Explorer 9 Developer Tools Deep Dive – Part 3: Debugging JavaScript . 另请参见MSDN文章Internet Explorer 9开发人员工具深入研究–第3部分:调试JavaScript

If you just want to see what's in the complex object without using full scale library, you can use such code: 如果您只想查看复杂对象中的内容而不使用完整比例的库,则可以使用以下代码:

var complexObject = { 
    "first field": "first value", "second": function() {
        alert("hello");
    }, 
    "third": ["I", "am", "an", "array"]
};

var complexObjectString = "";
for (var key in complexObject) {
    complexObjectString += key + ": " + complexObject[key] + "\n";
}
alert(complexObjectString);​

Live test case . 现场测试用例

In IE9 you need to be in standards mode to use the JSON object; 在IE9中,您需要处于标准模式才能使用JSON对象。 eg, you must have a doctype. 例如,您必须具有doctype。

This does not work: Live copy | 这不起作用: 实时复制 | source 资源

<html>
<head>
<meta charset=utf-8 />
<title>Test Page</title>
</head>
<body>
<p>Without a doctype</p>
<script>
(function() {

  try {
    var obj = {
      foo: "bar"
    };

    var str = JSON.stringify(obj);

    display("<code>" + str + "</code>");
  }
  catch (e) {
    display("Exception: " + (e.message || e.toString()));
  }

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
  }
})();
</script>
</body>
</html>

It fails with an error that JSON is undefined. 失败,并显示一个错误,即未定义JSON

If you add 如果添加

<!doctype html>

to the top, it works: Live copy | 到顶部,它的工作原理是: 实时复制 | source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM