我有一个链接,其中包含直接文件下载 URL。因此,当用户单击该链接时,该文件就会下载。
这是链接:
<a class="download-video" href="http://mylink.mp4?file=1&name=A Video"> Download</a>
我希望在下载此文件之前运行一些 JQuery。我能够找到解决方案,但我没有直接下载,而是被重定向到视频。我只想在没有任何重定向的情况下下载。这是我尝试过的:
$('a.download-video').click(function(event) {
var href = this.href;
//My JS Code
alert(href);
event.preventDefault();
window.location = href;
});
谢谢
最佳答案
您可以添加 download attribute到 anchor 标记,然后不要使用 preventDefault();
$('a.download-video').click(function(event) {
var href = this.href;
//My JS Code
alert(href);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="download-video" href="https://www.w3schools.com/html/mov_bbb.mp4?file=1&name=A Video" download> Download</a>
关于javascript - 首先运行 JQuery,然后在单击链接时下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42931351/