JavaScript function in href vs. onclick

后端 未结 15 1987
栀梦
栀梦 2020-11-22 07:17

I want to run a simple JavaScript function on a click without any redirection.

Is there any difference or benefit between putting the JavaScript call in the hr

15条回答
  •  北海茫月
    2020-11-22 08:09

    One more thing that I noticed when using "href" with javascript:

    The script in "href" attribute won't be executed if the time difference between 2 clicks was quite short.

    For example, try to run following example and double click (fast!) on each link. The first link will be executed only once. The second link will be executed twice.

    
    href
    onclick
    

    Reproduced in Chrome (double click) and IE11 (with triple click). In Chrome if you click fast enough you can make 10 clicks and have only 1 function execution.

    Firefox works ok.

提交回复
热议问题