首页 > 解决方案 > 为什么这个 console.log() 函数不起作用?

问题描述

<!DOCTYPE html>
<html>
<head>
<title>LearnJS</title>
</head>
<body>
    <script>
            console.log('hello World\nThis is me');
            alert("This is an \nalert.");
    </script>
</body>
</html>

我已经尝试过这段代码并在 TORCH borwser 中运行...显示的唯一输出是 alert 但它不显示 console.log 的输出...可能的解决方案是什么...我用过

document.write('hello World\nThis is me');

但是这段代码没有换行所以我应该使用console.log ...

标签: javascript

解决方案


它在这里工作正常:)。运行代码片段

<!DOCTYPE html>
<html>
<head>
<title>LearnJS</title>
</head>
<body>
    <script>
            console.log('hello World\nThis is me on console');
            alert("This is an \nalert.");
            document.write("This is an document.write.");
    </script>
</body>
</html>

笔记:

  • 开发人员console.log()用于在浏览器控制台上记录有用信息
  • document.write()通过向 DOM 添加其他内容来修改用户在浏览器中看到的内容。
  • alert()'s 用于提醒在浏览器上访问网页的最终用户。

注意如果您对 stackoverflow.com 如何console.log()在浏览器 div 上显示感到困惑。然后在这里查看https://stackoverflow.com/a/20256785/1138192它有点覆盖console.log()在浏览器 div 上显示消息的默认行为。希望这可以帮助 :)


推荐阅读