JavaScript function in href vs. onclick

后端 未结 15 2324
栀梦
栀梦 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 07:52

    bad:

    link text
    

    good:

    link text
    

    better:

    link text
    

    even better 1:

    link text
    

    even better 2:

    link text
    

    Why better? because return false will prevent browser from following the link

    best:

    Use jQuery or other similar framework to attach onclick handler by element's ID.

    $('#myLink').click(function(){ MyFunction(); return false; });
    

提交回复
热议问题