chrome中的console.log()无法正常工作

时间:2018-07-23 23:56:15

标签: javascript

我正在尝试在chrome中进行console.log。只是基本的东西。这是我的html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>hello</h1>
    <button onClick="clickme()">click</button>

    <script type="text/javascript" src="index.js"></script> 
</body>
</html>

现在我的js文件包含

var name="abc";
console.log("hello " + name);
 clickme=()=>{
   document.body.style.backgroundColor="red";

   alert(name);
     console.log("hello " + name);

} 

警报起作用。单击按钮后背景图像会发生变化,但是console.log无法正常工作。它可以在节点js中以及在边缘浏览器中使用时工作。 是否有缺少的东西。

2 个答案:

答案 0 :(得分:0)

https://codesandbox.io/s/p9zjw8lxrm

在这里,我有一个警报和console.log。您可以看到警报正在运行,但是console.log在关闭警报之前无法运行。

var name = "abc";
console.log("hello " + name);

document.getElementById("btn").onclick = function() {
  clickme();
};

const clickme = () => {
  document.body.style.backgroundColor = "red";

  alert(name);
  console.log("hello " + name);
};

第一次运行问题时,应该看到警报,然后是console.log

加载页面时,

首先您得到hello abc

然后在“警报”框中,单击“是”

然后hello abc

答案 1 :(得分:0)

确保已在Info下拉菜单中选中Default-levelsenter image description here

console.log()始终返回undefined(如果我输入错了,请改正我),但会在控制台中打印传递给它的值。