Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript—单击按钮时提供文件名的最佳方式_Javascript_Jquery_Html - Fatal编程技术网

Javascript—单击按钮时提供文件名的最佳方式

Javascript—单击按钮时提供文件名的最佳方式,javascript,jquery,html,Javascript,Jquery,Html,我尝试实现一个函数,该函数允许保存以前由Javascript代码计算的数据文件 HTML代码片段: <button id="btn-save" type="submit" >Save to file</button> $("#btn-save").click( function() { for (var i=0; i< indexArray; i+=1) { content += arrayLatitude[i]+" "+arrayLongitud

我尝试实现一个函数,该函数允许保存以前由Javascript代码计算的数据文件

HTML代码片段:

<button id="btn-save" type="submit" >Save to file</button>
$("#btn-save").click( function() {

  for (var i=0; i< indexArray; i+=1)
  {
   content += arrayLatitude[i]+" "+arrayLongitude[i];
   content += "\n";
  }

  // Build a data URI
  uri = "data:application/octet-stream," + encodeURIComponent(content);
  location.href=uri;
});
但我不知道如何将它调整为“
按钮”
“标记,而不是“
a
”标记

使用“
按钮”
”标记直接实现此功能的最佳方法是什么


感谢您的帮助

下载属性仅适用于
元素。作为一种解决方法,您可以使用CSS将您的
a
样式设置为一个
按钮,如下所示。感谢这些想法,它对我帮助很大
title = document.title+"_"+day+".html",
a = document.createElement("a");
a.setAttribute("href", uri);
a.setAttribute("download", title);
document.body.appendChild(a);