html - 有没有办法在 HTML 中制作一个按钮,每次单击它时,都会弹出一封新的预填充电子邮件?

标签 html

我正在尝试创建一个网站,其中有此按钮,每次单击它时,都会弹出一封新的预填充电子邮件。我知道如何链接电子邮件,但是有什么方法可以做到这一点,以便每次单击按钮时,它都是我已经预先填写的随机电子邮件?

这是我现在的代码:

    <li><a href="mailto:?"><i class="fa fa-envelope"></i></a></li>
var diffEmails = [
    // (code deleted off here because 
    // I didn't want to show the email/pre-filled email body for privacy reasons)
];

    function randomEmails() {
        var i = parseInt(Math.random() * diffEmails.length);
        location.href = diffEmails[i];
    }
<a href="mailto:" >onclick="randomEmails();"></a>

我使用了 font Awesome 中的社交媒体图标,并且不想删除它,所以我如何在 HTML 中使用它

最佳答案

您需要一个随机数:

var emails = [
  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="741534155a171b19" rel="noreferrer noopener nofollow">[email protected]</a>',
  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="076547652964686a" rel="noreferrer noopener nofollow">[email protected]</a>',
  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d3f0d39ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>',
  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="036743672d606c6e" rel="noreferrer noopener nofollow">[email protected]</a>',
  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acc9ecc982cfc3c1" rel="noreferrer noopener nofollow">[email protected]</a>',
  '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="234563450d404c4e" rel="noreferrer noopener nofollow">[email protected]</a>'
]


pickEmail = function() {
  let new_email;
  if (emails.length == 0) {
    // or you will have to generate new emails, and
    // push them into `emails`
    new_email = 'No email available.';
  } else {
    let idx = Math.floor((Math.random() * emails.length));
    // make sure this email will not be poped up again.
    new_email = emails.pop(idx);
  }
  document.getElementById('email').innerHTML = new_email;
}
<button onclick="pickEmail()">
Random Email
</button>

<p>
Email
</p>
<p id="email">

</p>

关于html - 有没有办法在 HTML 中制作一个按钮,每次单击它时,都会弹出一封新的预填充电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62942550/

相关文章:

javascript - Angular : $http 404 error handling

html - 在 Ruby 中解析网页的最佳方法是什么?

javascript - 在 html 中绘制 geojson 点

html - Foundation 4 选项卡部分可以在 Reveal 中显示吗?

html - 维护 CSS 关键帧中的最后颜色变化

html - 滚动条轨道比拇指细

javascript - 格式化 View 中的日期输入 Angular JS

javascript - Bootstrap-自动调整div内的字体

javascript - React.Js 未在 django 项目中渲染

html - float 菜单打扰了我的 float 内容