【问题标题】:is there a way to make a button in HTML that when every time you click it, a new pre-filled email will pop up?有没有办法在 HTML 中制作一个按钮,当您每次单击它时,都会弹出一个新的预填充电子邮件?
【发布时间】:2020-07-17 07:38:39
【问题描述】:

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

这是我现在的代码:

    <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 中使用它

【问题讨论】:

    标签: html


    【解决方案1】:

    你需要一个随机数:

    var emails = [
      'a@a.com',
      'b@b.com',
      'c@c.com',
      'd@d.com',
      'e@e.com',
      'f@f.com'
    ]
    
    
    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>

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 2023-02-06
      • 2021-11-12
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多