javascript - click() 在 Chrome 中工作正常,但在 IE11 中不行

标签 javascript html internet-explorer

我正在控制台上测试以下一段 Javascript 代码,然后将其包含到我的应用程序中(我在 SO 上找到了它)

pom = document.createElement('a')
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent("Hello World!"))
pom.setAttribute('download', "IEpomTest")
pom.style.display = 'none'
document.body.appendChild(pom)
pom.click()
document.body.removeChild(pom)

这在 Chrome 上运行良好,并且 pom.click() 下载带有“Hello World!”的文本文件。但是当我在 IE11 上尝试时,“pom.click()”将我带到这个 URL“data:text/plain;charset=utf-8,Hello%20World!”我收到错误消息“无法显示网页”。

有人知道我做错了什么吗?

最佳答案

IE 将尝试作为网站打开,更好的方法可能是检查 window.navigator.msSaveBlob 是否存在,如果存在,Using this method to download the file

关于javascript - click() 在 Chrome 中工作正常,但在 IE11 中不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43988862/

相关文章:

javascript - 我必须单击按钮两次才能在 React 中正确渲染状态

javascript - IE中的Ajax建议列表问题

javascript - 如何将 span 包裹在元素中的每个文本部分?

javascript - React props.children 不是数组

javascript - Jquery 通过选择选项数据值设置标记位置

javascript - 是否使用 php 或 javascript 方法来重定向页面

html - 在 HTML 中对齐小数点

php - MySQL 语法错误

wpf - 在 Internet Explorer 中运行的 MFC ActiveX 中承载的 wpf 窗口中的击键

internet-explorer - 如何导入不受信任的根证书?