為什么要去模擬window.open() 打開一個 新的窗口呢,因為有些瀏覽器默認會攔截 window.open, 當需要函數中打開新窗口時,接可以使用a標簽去模擬打開。
/** * a模擬window.open,不會被瀏覽器攔截 * @param {String} url a標簽打開的地址 * @param {String} id a標簽的ID * @param {String} targetType a標簽點擊打開的方式(當前頁面打開還是新窗口打開) */ openWindow: (url, targetType = '_blank', id = 'open', download = false) => { // 如果存在則刪除 if (document.getElementById(id)) { document.body.removeChild(document.getElementById(id)) } const a = document.createElement('a') a.setAttribute('href', url) if (download) { a.setAttribute('download', url) } a.setAttribute('target', targetType) a.setAttribute('id', id) document.body.appendChild(a) a.click() }