我有一个供医生使用的页面,他们可以在其中编写患者电子邮件,选择一堆文档,并通过“mailto”创建格式化电子邮件。这不适用于 Safari iOS。
我发现的问题是,如果从主屏幕(作为网络应用程序)启动,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;
}
暂时没有任何结果
最佳答案
您的代码存在一些问题:
a
从未被定义。为您的链接提供一个 ID,并使用document.getElementById()
选择 ID
- 您不能设置链接的
href
属性并使用双引号“中断”以将 onclick 属性添加到元素。相反: - 使用
.addEventListener()
添加click
事件。或者,如果您希望能够看到,您可以使用.setAttribute('onclick', 'window.location.href = "' + mailToLink + mailContent + '"; return false;');
使用浏览器的 DOM 检查器的 click 事件,但这并没有真正的意义。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>
关于javascript - 添加 onclick=window.location=mailto : where recipient is a variable in a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56206567/