Javascript SCRIPT1002:语法错误,第 1 行字符 6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11262911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:12:06  来源:igfitidea点击:

SCRIPT1002: Syntax Error, Line 1 Character 6

javascripthtmlinternet-explorersyntax

提问by Chud37

In the IE developer (F12) console, I've managed to get my pages to run without errors; all but one!

在 IE 开发人员 (F12) 控制台中,我设法让我的页面无错误地运行;除了一个!

SCRIPT1002: Syntax error
mypage.php, line 1 character 6

SCRIPT1002:语法错误
mypage.php,第 1 行字符 6

I am using IE9. Whats it's problem?

我正在使用 IE9。这是什么问题?

This is my code:

这是我的代码:

<!DOCTYPE html>
<head>
  <script type='text/javascript' src='/files/jquery-1.7.2.min.js'></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#donateButton").click(function() {
        alert('hey');
      });
    });
  </script>
</head>
<body>
  <a href="javascript:void();" id="donateButton">asdsadasd</a>
</body>

When I click on #donateButtonan error is produced. However, when I change javascript:void()to #then no error occurs any more. Why?

当我点击#donateButton一个错误产生。但是,当我更改javascript:void()#then 时不再发生错误。为什么?

回答by Rob W

"WAIT... does IE9 not like <a href="javascript:void();" id="donateButton">?? It seems thats the problem..?"
Comment by Chud37

“等等……IE9不喜欢<a href="javascript:void();" id="donateButton">吗??好像就是这个问题……?”
Chud37 的评论

Yes, thatis the problem. voidis an operator, not a function. Use javascript:void 0, javascript:void(0)or #. Even better, add event.preventDefault()to your function:

是的,就是问题所在。void是一个运算符而不是一个函数。使用javascript:void 0,javascript:void(0)#。更好的是,添加event.preventDefault()到您的函数中:

$('#donateButton').click(function(ev) {
    ev.preventDefault();
    alert('hello');
});