如何防止页面在锚点中使用mailto链接跳转到页面顶部? [英] How to prevent page from jumping to top of page with mailto link in an anchor?

查看:125
本文介绍了如何防止页面在锚点中使用mailto链接跳转到页面顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个锚点链接 mailto:example@email.com 作为href,但是当点击它时会跳转到页面顶部。我希望防止这种情况发生。

I have an anchor link with mailto:example@email.com as href, but it jumps to the top of the page when it is clicked. I want to prevent that from happening.

我在我的js中试过 e.preventDefault()有问题,它将不再打开电子邮件客户端。

I tried e.preventDefault() in my js, which works but has the problem that it won't open the email client anymore.

方案:

我有一个 联系我们页面底部的链接。它应该打开电子邮件客户端(它正在工作)。问题是每次点击它都会跳转到页面顶部。有没有办法防止这种情况发生?

I have a "Get in Touch" link at the bottom of my page. It should open the email client (which is working). The problem is that it jumps to top of page everytime it is clicked. Is there a way to prevent that from happening?

这里是锚链接的样子:

<a class="get-in-touch" href="mailto:email@gmail.com">Get In Touch</a>

这是我尝试过的。

$(".get-in-touch").on("click",function(e){
    e.preventDefault();
    window.location.href = $(this).attr("href");
});

但这不起作用。仍然跳到页面顶部。

but that didn't work. Still jumps to top of page.

推荐答案

而不是写作:

<a class="get-in-touch" href="mailto:email@gmail.com">Get In Touch</a>

写下以下内容:

<a class="get-in-touch" href="#" onclick="window.open('mailto:email@gmail.com')">Get In Touch</a>

这肯定会有效。

< a href =https://stackoverflow.com/questions/3868315/invoke-click-a-mailto-link-with-jquery-javascript>这里是关于你的问题的另一个stackoverflow问题。

Here is another stackoverflow question regarding your problem.

以下是 jsfiddle 的链接。

这篇关于如何防止页面在锚点中使用mailto链接跳转到页面顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆