以编程方式点击< a> -tag在Firefox中不起作用 [英] Programmatical click on <a>-tag not working in Firefox

查看:148
本文介绍了以编程方式点击< a> -tag在Firefox中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了来自jquery的 click() -function的问题。我用 document.createElement('a')创建一个< a> -element,并希望调用 click() -function有关此元素的信息。关于这个元素,我想创建一个Excel文件并将其保存在桌面上。

I have a problem with the click()-function from jquery. I create a <a>-element with document.createElement('a') and want call the click()-function about this element. About this element, I want to create an Excel-file and save this at the desktop.

我的代码:

$('body').on('click', '#test', function(event) {
    var link = document.createElement('a');
    link.download = 'test.xls';
    link.href = 'data:application/vnd.ms-excel;utf-8,test';
    link.click();
});

此功能在chrome下运行,但不在Firefox下运行。

This function work under chrome, but not under Firefox.

工作示例

有没有人知道为什么这不起作用?

Does anyone have any idea why that does not work?

推荐答案

在Firefox中,您可以将创建的元素显式添加到DOM中它会起作用:

In Firefox, you can explicitly add the created element to the DOM and it will work:

$('body').on('click', '#test', function(event) {
    var link = document.createElement('a');
    // Add the element to the DOM
    link.setAttribute("type", "hidden"); // make it hidden if needed
    link.download = 'test.xls';
    link.href = 'data:application/vnd.ms-excel;utf-8,test';
    document.body.appendChild(link);
    link.click();
    link.remove();
});

小提琴

这篇关于以编程方式点击&lt; a&gt; -tag在Firefox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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