简体   繁体   English

用<div>作为按钮并在单击时触发 mailto

[英]Use <div> as a button and trigger a mailto when it is clicked

I'm creating a custom button on my webpage which actually is a <div> , I want to trigger a mailto when the button is clicked.我正在我的网页上创建一个自定义按钮,它实际上是一个<div> ,我想在单击该按钮时触发一个 mailto。 What is the best way out?最好的出路是什么?

I've tried calling a javascript function using-onClick that looks like this -我试过使用-onClick 调用一个 javascript 函数,看起来像这样 -

function foo(){
    window.open("mailto:xyz@abc.com");
}

But that opens a new tab in Chrome first, and then asks for the relevant app to send out the email.但这首先会在 Chrome 中打开一个新标签,然后要求相关应用发送电子邮件。 This experience is different from what we generally get when we simply do a <a href=mailto:.....> in HTML.这种体验与我们通常在 HTML 中简单地做一个<a href=mailto:.....>时得到的体验不同。

I can also create a new document element in the JS function, and simulate a click like this -我也可以在JS函数中新建一个文档元素,模拟这样的点击——

function sendEmail() {
    var mail = 'mailto:contact@test.com';
    var a = document.createElement('a');
    a.href = mail;
    a.click();
};

But i'm not too sure if that's the right way!但我不太确定这是否是正确的方法! Anyone has a better solution?有人有更好的解决方案吗?

Try this, and tell me if works.试试这个,然后告诉我是否有效。 If not, I will delete answer.如果没有,我将删除答案。

<script>
function sendEmail() 
{
    window.location = "mailto:xyz@abc.com";
}
</script>
<div onclick="sendEmail();">Send e-mail</div>

我知道参加聚会非常晚,但是如何将这些答案组合成更简单、更实用的东西:

<div class="button" onclick="location.href='mailto:xyz@abc.com';">Send E-Mail</div>

Use an anchor tag but change the display property to block:使用锚标记但将显示属性更改为阻止:

HTML HTML

<a class="mailto" href="mailto:contact@test.com">Mail</a>

CSS CSS

.mailto{
  display:block;
  width:100px;
  height:20px;
}

This worked for me:这对我有用:

<script>
function sendEmail() 
{
    window.location.assign("mailto:xyz@abc.com");
}
</script>
<div onclick="sendEmail();">Send e-mail</div>

@Tony has used the same approach just assign has been added. @Tony已经用一样的方式assign已被添加。

Try this function and html.试试这个功能和 html。 It will open a new email client with.它将打开一个新的电子邮件客户端。

<div onclick="doMail();">


function doMail() {

    var email ="xyz@abc.com";
    location.href = "mailto:"+email;
}

In order to obfuscate your email from SPAM bots that scan website for emails, you can do the following,为了混淆来自扫描网站电子邮件的垃圾邮件机器人的电子邮件,您可以执行以下操作,

<div class="button" onclick="location.href='mail'+'to:xyz'+'@'+abc'+'.'+'com';">Send E-Mail</div>

and instead of the 'Send E-Mail' text you can place an image of your actual email address (screenshot) to make it more obvious.而不是“发送电子邮件”文本,您可以放置​​您实际电子邮件地址的图像(屏幕截图)以使其更加明显。

<div onclick="mailtoperformingFunction('inner');" id="divbutton">

<script type="text/javascript">
function mailtoperformingFunction()
{
}
</script>

尝试这个 :

<div class="button" href="javascript: void(0)" onclick="location.href='mailto:abc@xyz.com';">Click to Send email</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM