javascript - 为什么 document.write 不以数组格式写入?

标签 javascript

我正在尝试使用 document.write 编写嵌套数组,但它只写入 4 而不是 [[4]]。这可能是什么原因?

document.write([[4]])

最佳答案

我相信这是因为 [[4]] 的字符串表示形式是 4 :

console.log([[4]].toString())

您可以使用document.write(JSON.stringify([[4]]))做您想做的事:

console.log(JSON.stringify([[4]]))

或者,如果您需要更漂亮的格式:document.write('<pre>' + JSON.stringify([[4]],null,4) + '</pre>' )

document.write('<pre>' + JSON.stringify([[4]],null,4) + '</pre>' )

关于javascript - 为什么 document.write 不以数组格式写入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47950682/

相关文章:

javascript - canvas.toDataURL 返回空白图像

javascript - 将 'keydown' 事件监听器添加到文档(输入字段除外)

javascript - AngularJS 的子列表数量未知

javascript - 在 View 中访问 AngularJS 常量

javascript - 如何将 PHP 放入 Javascript 中?

javascript - jQuery 将 <span> 标签附加到 <h1> 标题

javascript - 每行至少搜索2个字母的正则表达式

javascript - jQuery .find() 和 .animate()

javascript - 客户端 Twitter 源

javascript - clearInterval 在点击时不会清除(但在悬停时工作正常)