console.log 只有在打开 "开发工具"(F12 用于切换打开和关闭)后才可用。 有趣的是,在你打开它之后,你可以关闭它,然后仍然通过调用 console.log 向它发帖,当你重新打开它时就会看到这些帖子。 我想这可能是个 Bug,可能会被修复,但我们拭目以待。
我可能会用这样的方法:
function trace(s) {
if ('console' in self && 'log' in console) console.log(s)
// the line below you might want to comment out, so it dies silent
// but nice for seeing when the console is available or not.
else alert(s)
}
甚至更简单:
function trace(s) {
try { console.log(s) } catch (e) { alert(s) }
}