var downloadAsFile = function(fileName, content) {
var a = document.createElement('a');
a.download = fileName;
a.href = 'data:application/vnd.ms-excel,'+encodeURIComponent(content);
a.click();
};Run Code Online (Sandbox Code Playgroud)
<button type="submit" id="output_button" onclick="downloadAsFile()">Download</button>Run Code Online (Sandbox Code Playgroud)
在下载 excel 文件时,我的屏幕位置向上移动到顶部。所以我希望屏幕位置保持在同一位置。
我知道如果我使用<a>标签,我可以使用“javascript:void(0)” ,但在这种情况下,我不知道如何添加。
有人有任何解决方案或可以通过其他方式解决吗?
您有三种* 方法可以停止滚动。在您的onclick函数中显式返回 false :
<button type="submit" id="output_button" onclick="downloadAsFile(); return false">Download</button>
Run Code Online (Sandbox Code Playgroud)
或者传递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();
};
Run Code Online (Sandbox Code Playgroud)
使用type="button"which 本质上不会尝试提交表单/处理链接:
<button type="button" id="output_button" onclick="downloadAsFile()">Download</button>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43 次 |
| 最近记录: |