javascript - .click() 在 IE11 中拒绝访问

标签 javascript html internet-explorer

当尝试调用 anchor 标记的 .click()自动点击 url 时。 代码在除 Internet Explorer v11 之外的所有浏览器中都可以正常工作。

我们将不胜感激。

var strContent = "a,b,c\n1,2,3\n";
var HTML_APS = strContent;
var data = new Blob([HTML_APS]);
var temp_link = document.createElement('a');
temp_link.href = URL.createObjectURL(data);
temp_link.download = "report_html.htm";
temp_link.type = "text/html";
temp_link.style = "display:none";
document.body.appendChild(temp_link);
if (confirm("Press a button!") == true) {
  temp_link.click();
  temp_link.remove();
}

这里是 fiddle .

最佳答案

对于 IE,您可以使用 navigator.msSaveOrOpenBlob

所以,跨浏览器,代码是

var strContent = "a,b,c\n1,2,3\n";
var HTML_APS = strContent;
var data = new Blob([HTML_APS]);

if (confirm("Press a button!") == true) {
  if (navigator.msSaveOrOpenBlob) {
    navigator.msSaveOrOpenBlob(data, "report_html.htm");
  } else {
    var temp_link = document.createElement('a');
    temp_link.href = URL.createObjectURL(data);
    temp_link.download = "report_html.htm";
    temp_link.type = "text/html";
    document.body.appendChild(temp_link);
    temp_link.click();
    temp_link.remove();
  }
}

关于javascript - .click() 在 IE11 中拒绝访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232980/

相关文章:

javascript - 如何使用 Javascript 将没有 ID 的按钮设置为 “click”?

javascript - IE DOM 操作性能

javascript - HTML 5 Canvas 上的 lineTo 无法正常工作

html - 嵌套无序列表的 IE 问题

css - 使用百分比宽度进行布局时如何修复 Internet Explorer 7 错误?

javascript - 将正则表达式模式从 Javascript 转换为 PCRE (perl)

javascript - 如何在 Angular2 中使用 jquery 和其他 .js 库

javascript - Reactjs 使用 Fluxible 从 store 获取事件

jquery - 附加表单元素,直到按下逗号

javascript - InternetExplorer 7 支持 postMessage() 吗?