Javascript SyntaxError: 预期的表达式,得到 '<',这是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31926326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: expected expression, got '<', what does that mean?
提问by nikel
I know this is asked a few times on S.O.
我知道这在 SO 上被问过几次
but none of the answers seem to match my situation . So, I have a a basic Html page which tries to use an external JS. The JS file tries to change the content of a paragraph defined in the HTML on a button click, but does not seem to work.
但似乎没有一个答案符合我的情况。所以,我有一个基本的 Html 页面,它尝试使用外部 JS。JS 文件试图在单击按钮时更改 HTML 中定义的段落的内容,但似乎不起作用。
I see the following errors in console :
我在控制台中看到以下错误:
1)SyntaxError: expected expression, got '<'
1)语法错误:预期的表达,得到'<'
2)ReferenceError: change is not defined
2)ReferenceError:未定义更改
JSFiddle showing exact source?(except that & tags are removed as jsfiddle - http://jsfiddle.net/p9ko4yde/
JSFiddle 显示确切来源?(除了 & 标签被删除为 jsfiddle - http://jsfiddle.net/p9ko4yde/
HTML Code :
HTML代码:
<h1> Numbers with external script:) </h1>
<p id="number">1</p>
<button type="button" onclick="change()">Toggle between 1 and 2</button>
<script src="myScript.js"></script>
</body>
</html>
JS Code :
JS代码:
<script type="text/javascript">
function change(){
var number = document.getElementById('number').innerHTML;
if(number == '1'){
document.getElementById('number').innerHTML='2';
}
else{
document.getElementById('number').innerHTML='1';
}
}
</script>
回答by Simon Robb
You don't need the <script>
tags when in an external .js file. Use these tags to embed a script inside HTML only.
<script>
在外部 .js 文件中时不需要标签。使用这些标签仅在 HTML 中嵌入脚本。
回答by Hayden Schiff
The syntax error is because you have <script>
tags in your JS file. When you put JavaScript in its own file like that, you don't need to surround it with script tags; that's HTML, and this is a JavaScript file.
语法错误是因为您<script>
的 JS 文件中有标签。当你像这样将 JavaScript 放在它自己的文件中时,你不需要用 script 标签包围它;那是 HTML,这是一个 JavaScript 文件。
The reference error is happening because, due to the syntax error, the JS file isn't executing correctly and so the change function never got created -- fixing the syntax error should also resolve this.
引用错误的发生是因为,由于语法错误,JS 文件没有正确执行,因此从未创建更改函数——修复语法错误也应该解决这个问题。
回答by trex005
In a js file, you do not use the HTML to declare that it is a js file. So, you can drop the script tag in myScript and change it to this :
在 js 文件中,您不使用 HTML 来声明它是一个 js 文件。因此,您可以在 myScript 中删除 script 标签并将其更改为:
function change(){
var number = document.getElementById('number').innerHTML;
if(number == '1'){
document.getElementById('number').innerHTML='2';
}
else{
document.getElementById('number').innerHTML='1';
}
}
回答by Aproram
Sometimes this error happens when the Javascript referenced file doesn't exist. Make sure that the file name is correct and you have the file in place. I know your problem is solved but maybe this answer solves someone else's :)
有时当 Javascript 引用的文件不存在时会发生此错误。确保文件名正确并且文件就位。我知道您的问题已解决,但也许这个答案解决了其他人的问题:)