Uncaught SyntaxError: Unexpected token ) when using void()

后端 未结 3 1395
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 18:20

I get this error and I\'ve managed to narrow it down to:

aaa

That

相关标签:
3条回答
  • 2021-01-01 18:21

    Is because void takes one argument. You want:

    <a href="javascript:void(0);" onclick="myFunction();">aaa</a>
    
    0 讨论(0)
  • 2021-01-01 18:28

    Use

    <a href="javascript:void(0);" onclick="myFunction();">aaa</a>
    

    void expects a parameter.

    There's an interesting discussion on using void(0) or other techniques here.

    0 讨论(0)
  • 2021-01-01 18:41

    void is an operator, not a function. It requires a single expression as its operand. () is not a valid expression. The correct syntax is:

    <a href="javascript:void 0;" onclick="myFunction();">aaa</a>
    

    You can put parentheses around 0, but they're not necessary, just as you don't need parentheses around 0 when writing 3 + 0.

    0 讨论(0)
提交回复
热议问题