Html anchor tag onclick() and href execute simultaneously

后端 未结 1 580
生来不讨喜
生来不讨喜 2021-01-04 05:23

Similar to HTML anchor link - href and onclick both? post, mine is:

Download link
         


        
相关标签:
1条回答
  • 2021-01-04 05:49

    Forget about the href and just do it all in the click function. You can navigate to another page after the update is complete. Here is my JQuery suggestion:

    HTML

    <a id="download" href="/tmp/download.mp3">Download link</a>
    

    JavaScript (with JQuery)

    $("#download").click(function(e){
        e.preventDefault();//this will prevent the link trying to navigate to another page
        var href = $(this).attr("href");//get the href so we can navigate later
    
        //do the update
    
        //when update has finished, navigate to the other page
        window.location = href;
    });
    

    NOTE: I added in an id for the a tag to ensure it can be selected accurately via JQuery

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