通过单击“ mailto”链接跳至页面顶部

时间:2018-07-04 13:17:27

标签: javascript browser scroll mailto

我使用此javaScript代码将“ mailto”添加到页面上的元素:

document.getElementById("mailTo").href = "mailto:?subject=look at this website&body=Hi,I found this website and thought you might like it "+ document.location.href

现在,当我单击链接时,它会很好地打开电子邮件,但它也会突然跳到页面顶部,有人可以告诉我为什么会这样吗?或如何阻止这种行为?

1 个答案:

答案 0 :(得分:0)

我将从链接中删除原始的onClick事件,并创建一个新函数来处理点击。那应该可以防止常规a-tag的不良行为,我想您的按钮是?

类似的东西

document.getElementById('mailTo').removeAttribute("onclick");

document.getElementById('mailTo').onclick = function(){
    location.href = "mailto:?subject=look at this website&body=Hi,I found this website and thought you might like it "+ document.location.href;
};

有关“ preventDefault();”的文档这也可能被证明是有价值的: https://www.w3schools.com/jsref/event_preventdefault.asp