xlearns / my-vue3-admin

0 stars 1 forks source link

js创建连接,实现跳转 #11

Open xlearns opened 2 years ago

xlearns commented 2 years ago

那么请问location.href跳转和给a标签设置点击事件有什么区别呢?

用a标签具体实现:

function fn(link){
  const $a = document.createElement("a");
  $a.setAttribute("href", link);
  $a.setAttribute("target", "_blank");
  $a.setAttribute("rel", "noreferrer noopener");
  $a.setAttribute("id", "external");
  document.getElementById("external") &&
    document.body.removeChild(document.getElementById("external"));
  document.body.appendChild($a);
  $a.click();
  $a.remove();
};

fn('baidu.com')