Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.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 从html表中追加数据,而不是另存为新文件_Javascript - Fatal编程技术网

Javascript 从html表中追加数据,而不是另存为新文件

Javascript 从html表中追加数据,而不是另存为新文件,javascript,Javascript,我在一个项目中工作,其中表中的数据将在单击按钮时更改。 如何将数据追加到新行的文件中,而不是保存到以下代码中的文件中: 函数下载\u csv(csv,文件名){ var-csvFile; var下载链接; csvFile=newblob([csv],{type:“text/csv”}); downloadLink=document.createElement(“a”); downloadLink.download=文件名; downloadLink.href=window.URL.create

我在一个项目中工作,其中表中的数据将在单击按钮时更改。 如何将数据追加到新行的文件中,而不是保存到以下代码中的文件中:

函数下载\u csv(csv,文件名){
var-csvFile;
var下载链接;
csvFile=newblob([csv],{type:“text/csv”});
downloadLink=document.createElement(“a”);
downloadLink.download=文件名;
downloadLink.href=window.URL.createObjectURL(csvFile);
downloadLink.style.display=“无”;
document.body.appendChild(下载链接);
下载链接。单击();}
函数将表格导出为csv(html,文件名){
var csv=[];
var rows=document.queryselectoral(“表tr”);
对于(变量i=0;i
您不能在客户端追加/修改文件。您必须使用新数据生成新文件。

您不能在客户端追加/修改文件。您必须使用新数据生成一个新文件

function download_csv(csv, filename) {
var csvFile;
var downloadLink;

csvFile = new Blob([csv], {type: "text/csv"});
downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();}

function export_table_to_csv(html, filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
for (var i = 0; i < rows.length; i++) {
    var row = [], cols = rows[i].querySelectorAll("td, th");
    for (var j = 0; j < cols.length; j++) 
        row.push(cols[j].innerText);
    csv.push(row.join(","));        
}
download_csv(csv.join("\n"), filename);}
document.querySelector("button").addEventListener("click", function () {
var html = document.querySelector("table").outerHTML;
export_table_to_csv(html, "table.csv");});