通过点击 "mailto" link 跳转到页面顶部
By clicking "mailto" link jumps to top of the page
我使用此 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
现在,当我单击 link 时,它可以正常打开电子邮件,但它也会突然跳到页面顶部,谁能告诉我为什么会这样?或者我怎样才能阻止这种行为?
我将从 link 中删除原始的 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
我使用此 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
现在,当我单击 link 时,它可以正常打开电子邮件,但它也会突然跳到页面顶部,谁能告诉我为什么会这样?或者我怎样才能阻止这种行为?
我将从 link 中删除原始的 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