创建一个a标签并触发其事件

有个项目需求,需要下载一个文件时名称不乱码显示,前段解决方案,使用a标签新增download 字段,由于需要模拟a标签,以下以做记录。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

var a = document.createElement('a');
a.setAttribute('href', href);
a.setAttribute('target', '_blank');
a.setAttribute('id', 'startTelMedicine');
// 防止反复添加
if(document.getElementById('startTelMedicine')) {
	document.body.removeChild(document.getElementById('startTelMedicine'));
}
document.body.appendChild(a);
a.click();