.click()在IE11中拒绝访问

.click()在IE11中拒绝访问

问题描述:

尝试调用anchor标记的.click()auto click url。 代码在所有浏览器中都正常工作,除了Internet Explorerv11.click()在IE11中拒绝访问

任何帮助将不胜感激。

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

+0

的IE浏览器,使用'navigator.msSaveOrOpenBlob' - https://jsfiddle.net/ hcqn9m5a/3/ –

+0

这里没有jQuery代码,为什么你有这个标签? – Barmar

对于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(); 
    } 
} 
+0

因此对于IE“var data = new Blob([HTML_APS]);”这不会起作用代码“navigator.msSaveOrOpenBlob(data,”report_html.htm“)”应该被执行? – skoley

+1

看看代码...'var data = new Blob([HTML_APS]);'是为所有浏览器...它只是如何下载它不同 –

+0

我的不好感谢解决方案:) – skoley

当使用下载属性锚点时,这表示浏览器应该下载锚点指向的资源而不是导航到它。
它不支持IE11。 仅供参考click here

根据this SO答案,'下载'属性尚未在Internet Explorer中实现。

下载属性未在Internet Explorer中实现。

http://caniuse.com/download

对于Internet Explorer,可以使用 “另存为” 命令。