What is the Difference between onclick and href="[removed]function name?

前端 未结 6 1689
别跟我提以往
别跟我提以往 2021-02-10 12:20

Is there any difference between

1 : Link1

and

2 : 

        
6条回答
  •  长发绾君心
    2021-02-10 12:49

    An href="javascript: doSomething" means you do not have a url to fallback to if the user doesn't have js enabled.

    Therefore, setting href="something.html" and onclick="return doSomething()" is usually considered better because if js is disabled, you can navigate to a new page, but if js is enabled, you can return false to prevent navigation to the link and display something within the same page without a page refresh.

    Even better, don't add the onclick inline, just add js handlers when the page loads. That's the unobtrusive way

提交回复
热议问题