Html 锚标记 onclick() 和 href 同时执行 [英] Html anchor tag onclick() and href execute simultaneously

查看:24
本文介绍了Html 锚标记 onclick() 和 href 同时执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于 HTML 锚链接 - href 和 onclick 两者?帖子,我的是:

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

<a href="/tmp/download.mp3" onclick="update();">Download link</a>

update() 方法会向服务器发送另一个 HTTP GET 方法来更新已下载的文件并更新数据库.从 HTML 代码来看,有 2 个 GET 请求被一起触发,只有一个会从服务器获得响应(href 下载响应).update() 没有被执行.有没有什么方法可以让我同时执行两个 GET 方法?欢迎提供 Javascript 和 Jquery 建议!

The update() method would send another HTTP GET method to the server to update the which file has been downloaded and update the database. From the HTML code, there are 2 GET requests being triggered together and only ONE will get the response (the href download response) from the server. The update() is not being executed. Is there any method that i can get both GET methods to be executed? Javascript and Jquery suggestions are welcomed!

推荐答案

忘掉 href 并在点击功能中完成所有操作.更新完成后,您可以导航到另一个页面.这是我的 JQuery 建议:

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(使用 JQuery)

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;
});

注意:我为 a 标签添加了一个 id 以确保它可以通过 JQuery 准确选择

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

这篇关于Html 锚标记 onclick() 和 href 同时执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆