最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery - Uncaught SyntaxError: Unexpected token ) - Stack Overflow

programmeradmin1浏览0评论

I have - what I think - should be a very simple jquery script. When the register_hyperlink anchor is clicked, I am presented with the alert box, as expected; but after I click the OK, I am getting an error: Uncaught SyntaxError: Unexpected token )

The code block is below. I can't see anywhere that there are unbalanced parenthesis as the error states. The code below is inside the element of my html page.

<script type="text/javascript" src=".0.1.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $("#register_hyperlink").click(function() {
      alert('Hello');
     });
  });
</script>

Does anyone have any idea how to go about debugging this? I've been at it for a while now, but am having zero luck.

Thanks!

I have - what I think - should be a very simple jquery script. When the register_hyperlink anchor is clicked, I am presented with the alert box, as expected; but after I click the OK, I am getting an error: Uncaught SyntaxError: Unexpected token )

The code block is below. I can't see anywhere that there are unbalanced parenthesis as the error states. The code below is inside the element of my html page.

<script type="text/javascript" src="http://ajax.aspnetcdn./ajax/jQuery/jquery-2.0.1.min.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $("#register_hyperlink").click(function() {
      alert('Hello');
     });
  });
</script>

Does anyone have any idea how to go about debugging this? I've been at it for a while now, but am having zero luck.

Thanks!

Share Improve this question asked Jun 5, 2013 at 3:39 BrettBrett 12k35 gold badges135 silver badges221 bronze badges 5
  • 2 The console error log will have some details about the source of the error and a stacktrace can you share them too? is there a onclick attribute in the a element – Arun P Johny Commented Jun 5, 2013 at 3:42
  • 2 That code is fine. The problem is elsewhere. – Matt Ball Commented Jun 5, 2013 at 3:42
  • Only thing i can think of is trying .on('click', function(){ instead, you are using the most recent jquery lib , maybe .click is deprecated by now – Jay Rizzi Commented Jun 5, 2013 at 3:44
  • in jsfiddle works perfectly – wm.p1us Commented Jun 5, 2013 at 3:44
  • @JayRizzi No, .click() isn't deprecated...a simple check in the jQuery docs would confirm that. It's just a shortcut for .on("click". And being deprecated doesn't mean it's not available, it means it will be removed in a future build. – Ian Commented Jun 5, 2013 at 3:51
Add a ment  | 

3 Answers 3

Reset to default 5

I had:

<a id="register_hyperlink" href="javascript:void();">Register an account</a>

I changed it to:

<a id="register_hyperlink" href="javascript:void(0);">Register an account</a>

So that explains it :-)

What have you got in the href attribute of your anchor? Alternatively do you have a an onClick attribute in the anchor or are you catching it anywhere else?

You are not preventing the default behaviour of the anchor and you may have a syntax error in the href. That would be my first guess.

You could change your posted function to:

$(document).ready(function () {
    $("#register_hyperlink").click(function(e) {
        e.preventDefault();
        e.stopPropagation();
        alert('Hello');
    });
});

If you test with this function you may find, as Matt Ball points out that your problem was indeed elsewhere

you could use

<a id="register_hyperlink" href="javascript:;">Register an account</a>
发布评论

评论列表(0)

  1. 暂无评论