js 模拟点击方式打开URL

in 前端JavaScript with 0 comment
const openUrl = (url, _blank = false) => {
    setTimeout(() => {
        const id = 'openUrl';
        var a = document.createElement('a');
        a.setAttribute('href', url);
        a.setAttribute('class', "openUrl");
        _blank && a.setAttribute('target', '_blank');
        a.setAttribute('id', id);
        document.body.appendChild(a);
        a.click();
        document.querySelector(`#${id}`).remove();
    }, 100);
};
Comments are closed.