Javascript 意外的标记 }
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4491533/
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
Unexpected token }
提问by Tanner Ottinger
I have a script to open a model window.. Chrome gives me "Uncaught SyntaxError: Unexpected token }" on a line that doesn't even have a closing curly brace.
我有一个打开模型窗口的脚本.. Chrome 给了我 "Uncaught SyntaxError: Unexpected token }" 在一条甚至没有右花括号的行上。
Here is the portion of the script that has the error:
这是脚本中出现错误的部分:
function showm(id1){
window.onscroll=function(){document.getElementById(id1).style.top=document.body.scrollTop;};
document.getElementById(id1).style.display="block";
document.getElementById(id1).style.top=document.body.scrollTop;
}
Does anybody have any ideas on this? Any help is appreciated.
有人对此有任何想法吗?任何帮助表示赞赏。
回答by Day
Try running the entirescript through jslint. This may help point you at the cause of the error.
尝试通过jslint运行整个脚本。这可能有助于指出错误的原因。
EditOk, it's not quite the syntax of the script that's the problem. At least not in a way that jslint can detect.
编辑好的,问题不完全是脚本的语法。至少不是 jslint 可以检测到的方式。
Having played with your live code at http://ft2.hostei.com/ft.v1/, it looks like there are syntax errors in the generated codethat your script puts into an onclick
attribute in the DOM. Most browsers don't do a very good job of reporting errors in JavaScript run via such things (what is the file and line number of a piece of script in the onclick
attribute of a dynamically inserted element?). This is probably why you get a confusing error message in Chrome. The FireFox error message is different, and also doesn't have a useful line number, although FireBug does show the code which causes the problem.
在http://ft2.hostei.com/ft.v1/ 上玩过您的实时代码后,您的脚本将其放入DOM 中的属性的生成代码中似乎存在语法错误onclick
。大多数浏览器都没有很好地报告通过这些东西运行的 JavaScript 中的错误(onclick
动态插入元素的属性中的一段脚本的文件和行号是什么?)。这可能就是您在 Chrome 中收到令人困惑的错误消息的原因。FireFox 错误消息是不同的,也没有有用的行号,尽管 FireBug 确实显示了导致问题的代码。
This snippet of code is taken from your edit
function which is in the inline script block of your HTML:
此代码片段取自edit
HTML 内联脚本块中的函数:
var sub = document.getElementById('submit');
...
sub.setAttribute("onclick", "save(\""+file+"\", document.getElementById('name').value, document.getElementById('text').value");
Note that this sets the onclick
attribute of an element to invalidJavaScript code:
请注意,这onclick
会将元素的属性设置为无效的JavaScript 代码:
<input type="submit" id="submit" onclick="save("data/wasup.htm", document.getElementById('name').value, document.getElementById('text').value">
The JS is:
JS是:
save("data/wasup.htm", document.getElementById('name').value, document.getElementById('text').value
Note the missing close paren to finish the call to save
.
请注意缺少的关闭括号以完成对save
.
As an aside, inserting onclick
attributes is not a very modern or clean way of adding event handlers in JavaScript. Why are you not using the DOM's addEventListener
to simply hook up a function to the element? If you were using something like jQuery, this would be simpler still.
onclick
顺便说一句,插入属性并不是在 JavaScript 中添加事件处理程序的一种非常现代或干净的方式。为什么不使用 DOMaddEventListener
来简单地将函数连接到元素?如果你使用像 jQuery 这样的东西,这会更简单。
回答by Shadow Wizard is Ear For You
You have endless loop in place:
你有无限循环:
function save() {
var filename = id('filename').value;
var name = id('name').value;
var text = id('text').value;
save(filename, name, text);
}
No idea what you're trying to accomplish with that endless loop but first of all get rid of it and see if things are working.
不知道你想用那个无限循环来完成什么,但首先摆脱它,看看事情是否正常。