Programmatical click on -tag not working in Firefox

前端 未结 5 627
醉话见心
醉话见心 2020-12-02 17:57

I have a problem with the click()-function from jquery. I create a -element with document.createElement(\'a\') and want call

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 18:50

    You don't have to add the element to the DOM, even in FireFox. Replace the .click() method with the following code:

    link.dispatchEvent(new MouseEvent(`click`, {bubbles: true, cancelable: true, view: window}));
    

    $('button').on('click', function(event) {
        var link = document.createElement('a');
        link.download = 'test.xls';
        link.href = 'data:application/vnd.ms-excel;utf-8,test';
        link.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true, view: window}));
    });
    
    

提交回复
热议问题