调整下载按钮以下载两个表格
HTML示例代码:
JavaScript示例代码:
const downloadBtn = document.getElementById('downloadBtn');
downloadBtn.addEventListener('click', () => {
const table1CSV = convertTableToCSV(document.getElementById('table1'));
const table2CSV = convertTableToCSV(document.getElementById('table2'));
const combinedCSV = ${table1CSV}\n${table2CSV}
;
downloadCSV(combinedCSV);
});
// 将HTML table转换为CSV格式
function convertTableToCSV(table) {
const rows = table.querySelectorAll('tr');
return Array.from(rows, row => Array.from(row.cells, cell => "${cell.innerText}"
).join(',')).join('\n');
}
// 下载CSV文件 function downloadCSV(csv) { const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.setAttribute("href", url); link.setAttribute("download", "data.csv"); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); }
上一篇:AdjustDefaultZoominlightweightcharts
下一篇:adjusted_rand_score函数中出现的问题:longlong_scalars溢出错误(tp+fp)*(fp+tn)。