我有此代码,但它仅在Chrome中有效,有什么办法可以使其在IE中工作。它应该正在下载CSV文件。或者是否有其他方法可以使用IE10导出csv文件
$('#download').on('click', function () {
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('#example1', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.click();
}
});
<table id="example1" border="1" style="background-color:#FFFFCC" width="0%" cellpadding="3" cellspacing="3">
<tr>
<th>Title</th>
<th>Name</th>
<th>Phone</th>
</tr>
<tr>
<td>Mr.</td>
<td>John</td>
<td>07868785831</td>
</tr>
<tr>
<td>Miss</td>
<td><i>Linda</i></td>
<td>0141-2244-5566</td>
</tr>
<tr>
<td>Master</td>
<td>Jack</td>
<td>0142-1212-1234</td>
</tr></table>
<a href="#" id="download">Download</a>
这是JSFIDDLE DEMO
更新:
Example 3.1可以正常工作,但是我不知道如何将上面的代码转换为:
Working Example
更新::
$('#download').on('click', function () {
var csvContent = $('#example1'); //here we load our csv data
var blob = new Blob([csvContent], {
type: "text/csv;charset=utf-8;",
});
navigator.msSaveBlob(blob, "filename.csv")
});
});
我收到错误0x800a139e-JavaScript运行时错误:InvalidStateError
最佳答案
在IE11上,
innerHTML正常工作。
<html>
<head>
<script lang="javascript">
function download(){
var csvContent = document.getElementById("example1").innerHTML; //here we load our csv data
var blob = new Blob([csvContent], {
type: "text/csv;charset=utf-8;",
});
var blob = new Blob([csvContent], {
type: "text/csv;charset=utf-8;",
});
navigator.msSaveBlob(blob, "filename.csv")
}
</script>
</head>
<body>
<a href="#" onclick="download()">Download</a>
<table id="example1" border="1" style="background-color:#FFFFCC" width="0%" cellpadding="3" cellspacing="3">
<tr>
<th>Title</th>
<th>Name</th>
<th>Phone</th>
</tr>
<tr>
<td>Mr.</td>
<td>John</td>
<td>07868785831</td>
</tr>
<tr>
<td>Miss</td>
<td><i>Linda</i></td>
<td>0141-2244-5566</td>
</tr>
<tr>
<td>Master</td>
<td>Jack</td>
<td>0142-1212-1234</td>
</tr></table>
</body>
</html>
关于javascript - 如何在Internet Explorer中支持Chrome文件导出功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21622500/