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