javascript - 通过点击“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
现在,当我点击链接时,它打开电子邮件就好了,但它也突然跳到页面顶部,谁能告诉我为什么会这样?或者我怎样才能阻止这种行为?
解决方案
我会从链接中删除原始的 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
推荐阅读
- reactjs - 选择删除链接后如何在 React 中重新渲染数据?
- haskell - 数据类相当于`_1`风格的元组镜头快捷方式?
- python - 熊猫在具有 DatetimeIndex 的特定列上左合并
- react-native - 如何使用变量使用 useState 挂钩更改状态
- mongodb - MongoDB 管道聚合中的输出不一致
- regex - <|> 在这个解析器中的顺序真的很重要吗?
- authentication - Google Sheet 联合表的 Tableau Bigquery 访问问题
- apache-flink - 我们可以使用 apache flink 实时生成 pef/excel 文件吗?
- angular - 仅使用 Angular 框架(不是 AngularJS 和中间层)对 Oracle 云进行身份验证
- django - 如何在用户配置文件模型中上传图像,同时与 django rest 框架中的用户模型具有一对一的关系?