JavaScript function in href vs. onclick

后端 未结 15 2021
栀梦
栀梦 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:06

    the best way to do this is with:

    
    

    The problem is that this WILL add a hash (#) to the end of the page's URL in the browser, thus requiring the user to click the back button twice to go to the page before yours. Considering this, you need to add some code to stop event propagation. Most javascript toolkits will already have a function for this. For example, the dojo toolkit uses

    dojo.stopEvent(event);
    

    to do so.

提交回复
热议问题