下载的点击跨度的HTML文件 [英] Download a html file on click of span

查看:205
本文介绍了下载的点击跨度的HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载上点击一个跨度的HTML文件。

I want to download a HTML file on click of a span.

我没有使用了任何锚标记。我想这样做简单的点击一个跨度。

I am not using any anchor tag in this. I want to do this on simple click of a span.

地址是有.html扩展名。和URL到HTML文件位于另一个域(亚马逊S3)

Url is having .html extension. And the url to html file is on another domain(Amazon s3)

如何做到这一点在JavaScript中,如锚标记它会在里面容易,我会写属性下载。

How to achieve this in JavaScript, as in anchor tag it would have been easy where I would have written attribute 'download' in it.

推荐答案

感谢您的答案和注释。

我现在能够被侵犯问题的prerequisite但下载的文件(即锚标记使用)

I am now able to download the file, however by violating the prerequisite of the question(i.e. anchor tag usage)

我跟着这个。在下面的链接的方法做这项工作

I followed this. The methods below in the link does the job

function SaveToDisk(fileUrl, fileName) {
    var hyperlink = document.createElement('a');
    hyperlink.href = fileUrl;
    hyperlink.target = '_blank';
    hyperlink.download = fileName || fileUrl;

    var mouseEvent = new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
    });

    hyperlink.dispatchEvent(mouseEvent);
    (window.URL || window.webkitURL).revokeObjectURL(hyperlink.href);
}

不过,我现在需要摧毁下载链接后创建的锚标记。

However I now need to destroy the anchor tag created after downloading the link.

这篇关于下载的点击跨度的HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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