Skip to content

Instantly share code, notes, and snippets.

@ClausClaus
Last active April 4, 2020 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClausClaus/b6768cb0253a12e76bc54b7c7b4593ca to your computer and use it in GitHub Desktop.
Save ClausClaus/b6768cb0253a12e76bc54b7c7b4593ca to your computer and use it in GitHub Desktop.
点击a标签打开新页面
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