意外的标记 } [英] Unexpected token }

查看:329
本文介绍了意外的标记 }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打开模型窗口的脚本.. Chrome在一条甚至没有结束大括号的行上给了我Uncaught SyntaxError:Unexpected token}。

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.

以下是包含错误的脚本部分:

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.

推荐答案

尝试通过整个脚本: //jslint.com/rel =noreferrer> jslint 。这可能有助于指出错误原因。

Try running the entire script through jslint. This may help point you at the cause of the error.

编辑好的,这不是脚本的语法问题。至少不是jslint可以检测的方式。

Edit Ok, it's not quite the syntax of the script that's the problem. At least not in a way that jslint can detect.

http://ft2.hostei.com/ft.v1/ ,您的脚本会在生成的代码中出现语法错误进入DOM中的 onclick 属性。大多数浏览器都不能很好地报告通过这些事情运行的JavaScript中的错误( onclick 属性中的一段脚本的文件和行号是什么一个动态插入的元素?)。这可能是您在Chrome中收到令人困惑的错误消息的原因。 FireFox错误消息不同,也没有有用的行号,尽管FireBug确实显示导致问题的代码。

Having played with your live code at http://ft2.hostei.com/ft.v1/, it looks like there are syntax errors in the generated code that 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.

这段代码被采用来自您的编辑函数,该函数位于HTML的内联脚本块中:

This snippet of code is taken from your edit function which is in the inline script block of your HTML:

var sub = document.getElementById('submit');
...
sub.setAttribute("onclick", "save(\""+file+"\", document.getElementById('name').value, document.getElementById('text').value");

注意这会设置 onclick 元素的属性为无效 JavaScript代码:

Note that this sets the onclick attribute of an element to invalid JavaScript code:

<input type="submit" id="submit" onclick="save("data/wasup.htm", document.getElementById('name').value, document.getElementById('text').value">

JS是:

save("data/wasup.htm", document.getElementById('name').value, document.getElementById('text').value

注意缺少关闭完成对保存的调用。

Note the missing close paren to finish the call to save.

除此之外,插入 onclick 属性并不是一种在JavaScript中添加事件处理程序的非常现代或干净的方式。为什么不使用DOM addEventListener 简单地将函数挂钩到元素?如果你使用的是像jQuery这样的东西,那就更简单了。

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.

这篇关于意外的标记 }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆