Last active
April 4, 2020 10:07
-
-
Save ClausClaus/b6768cb0253a12e76bc54b7c7b4593ca to your computer and use it in GitHub Desktop.
点击a标签打开新页面
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function openNewPage(url) { | |
let a = document.createElement('a'); | |
a.href = url; | |
a.target="_blank"; | |
a.innerHTML = "链接"; | |
let d = a.getAttribute('href'); | |
try { | |
let e = document.createEvent('MouseEvents') | |
e.initEvent('click', true, true) //模拟点击操作 | |
d.dispatchEvent(e) | |
} catch (e) { | |
window.open(url) | |
} | |
a.remove() // 点击后移除该对象 | |
} | |
openNewPage('https://www.baidu.com'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment