Javascript chrome无法运行console.log()未捕获的语法错误:无效或意外的令牌
这是我的html代码Javascript chrome无法运行console.log()未捕获的语法错误:无效或意外的令牌,javascript,console.log,Javascript,Console.log,这是我的html代码 <body> <script src="../js/myjscode.js"></script> </body> 如果我输入console.loghello world;关于myjscode.js 完美输出是世界吗 但是如果我打字 function firstFunction() { console.log(" hello world "); } firstFu
<body>
<script src="../js/myjscode.js"></script>
</body>
如果我输入console.loghello world;关于myjscode.js
完美输出是世界吗
但是如果我打字
function firstFunction() {
console.log("
hello world ");
}
firstFunction();
关于myjscode.js
它显示在chrome控制台上
未捕获的语法错误:无效或意外标记
我做错了什么?必须使用转义序列来指示字符串中的换行符;就你而言
"\nhello world"
这是因为当您在console.log内的新行中键入字符串时,会有一个新行回车,因此会得到未捕获的SyntaxError:无效或意外的标记。要解决此问题,请使用反引号而不是引号: 函数优先函数{ console.log` 你好,世界`; }
第一功能 在示例中,必须将console.log文本放在一行中 像这样:
function firstFunction() {
console.log("hello world");
}
firstFunction();
您可以A:将console.log放在同一行中,或者B:将其转换为按钮。或者两者都做 Javascript HTML
如果在引用之后执行多行操作,则必须连接 这项工作:
console.log("" +
"hello world "
);
或
或:
将其保存在单行“console.log hello world;”不要包装
function firstFunction() {
console.log("hello world");
}
firstFunction();
<button onclick="firstFunction()">click me</button>
console.log("" +
"hello world "
);
console.log(
"hello world"
);
console.log(
"hello" +
"world"
);