微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – .click()在IE11中拒绝访问权限

尝试调用标记的.click()以自动单击该网址时.
代码在除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,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();
  }
}

原文地址:https://www.jb51.cc/js/150876.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐