gpt4 book ai didi

javascript - 使用javascript下载excel文件后停止屏幕位置上升

转载 作者:行者123 更新时间:2023-11-30 09:20:53 25 4
gpt4 key购买 nike

    var downloadAsFile = function(fileName, content) {
var a = document.createElement('a');
a.download = fileName;
a.href = 'data:application/vnd.ms-excel,'+encodeURIComponent(content);
a.click();
};
 <button type="submit" id="output_button" onclick="downloadAsFile()">Download</button>

在下载 excel 文件时,我的屏幕位置向上移动。所以我希望屏幕位置保持在同一位置。

我知道如果我使用 <a> 我可以使用“javascript:void(0)”标签,但在这种情况下,我不知道如何添加。

有人有任何解决方案或可以用其他方式解决吗?

最佳答案

您可以通过三种*方式停止滚动。在您的 onclick 函数中显式返回 false:

<button type="submit" id="output_button" onclick="downloadAsFile(); return false">Download</button>

或者将 event 传递给您的函数,然后调用 event.preventDefault():

<button type="submit" id="output_button" onclick="downloadAsFile(event, 'file', 'content'); return false">Download</button>

var downloadAsFile = function(evt, fileName, content) {
evt.preventDefault();
var a = document.createElement('a');
a.download = fileName;
a.href = 'data:application/vnd.ms-excel,'+encodeURIComponent(content);
a.click();
};

使用本质上不尝试提交表单/处理链接的 type="button":

<button type="button" id="output_button" onclick="downloadAsFile()">Download</button>

关于javascript - 使用javascript下载excel文件后停止屏幕位置上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51779936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com