我有一个供医生使用的页面,他们可以在其中写患者电子邮件,选择一系列退回文件,然后通过“ mailto”创建格式化的电子邮件。这不适用于Safari iOS。
我发现的问题是,如果从主屏幕(作为Web应用程序)启动,mailto不适用于Safari iOS。它使用onclick = window.location可以运行,但我尝试在我的函数上实现它,但无法正常工作
所以我正在尝试实现这样的东西:
<a href="#" onclick="window.location='mailto:me@any.com?subject=Docs&body=Hallo%20you%20and%20links'; return false;" class="noHighlight">Write and email</a>
在我的职能中:
function buildMailto(recipient, subject, body) {
var mailToLink = "mailto:" + recipient + "?";
var mailContent = "Subject=" + subject + "&";
mailContent += "cc=testemail@gmail.com&";
mailContent += "body="
mailContent += encodeURIComponent(body);
a.href = mailToLink + mailContent;
}
我正在尝试编辑:
function buildMailto(recipient, subject, body) {
var mailToLink = " '#' onclick=\"window.location='mailto:" + recipient +"?";
var mailContent = "Subject=" + subject + "&";
mailContent += "cc=testemail@gmail.com&";
mailContent += "body="
mailContent += encodeURIComponent(body);
mailContent += "return false; class='noHighlight' ";
a.href = mailToLink + mailContent;
}
目前没有任何结果
答案 0 :(得分:0)
您的代码存在一些问题:
a
从未定义。给您的链接一个ID,并使用document.getElementById()
href
属性并使用双引号“中断”以将onclick属性添加到元素。相反:click
添加.addEventListener()
事件。另外,如果您希望能够使用浏览器的DOM检查器查看click事件,则可以使用.setAttribute('onclick', 'window.location.href = "' + mailToLink + mailContent + '"; return false;');
,但这实际上没有任何意义。
function buildMailto(recipient, subject, body) {
var mailToLink = "mailto:" + recipient +"?";
var mailContent = "Subject=" + subject + "&";
mailContent += "cc=testemail@gmail.com&";
mailContent += "body="
mailContent += encodeURIComponent(body);
document.getElementById('myLink').addEventListener('click', function() {
window.location.href = mailToLink + mailContent;
return false;
});
}
buildMailto('foo@bar.baz', 'a subject', 'mail content');
<a href="#" id="myLink" class="noHighlight">Write and email</a>
答案 1 :(得分:0)
在您的buildMailto
函数中,您没有指定什么是a
。单击锚链接时,不要更新onclick
属性值,而应构建一个mailto URL并更新window.location.href
。这是一个示例:
<a href="#" id="email-link" class="noHighlight">Write and email</a>
锚链接的事件侦听器:
document.querySelector('#email-link').addEventListener('click', function (e) {
e.preventDefault();
buildMailto('me@any.com', 'Docs', 'Hi, there!');
});
将您的buildMailto
更改为以下内容:
function buildMailto(recipient, subject, body) {
var mailLink = "mailto:" + recipient + "?";
mailLink += "Subject=" + subject + "&";
mailLink += "cc=testemail@gmail.com&";
mailLink += "body="
mailLink += encodeURIComponent(body);
window.location.href = mailLink;
}
答案 2 :(得分:-1)
尝试将可操作对象嵌入到表单中。像这样:
<form action="mailto:me@any.com?subject=Docs&body=Hallo%20you%20and%20links" method="GET">
<button class="noHighlight" type="submit">Write and email</a>
</form>