[英]Javascript variable into href=“mailto:” string
I am trying to create a simple page that can take some user inputs and print them back out, and I have found a way that is working fine. 我正在尝试创建一个简单的页面,可以采取一些用户输入并打印出来,我找到了一种工作正常的方法。 But there is one thing I can not manage to do, and that is to take the E-post input from the user and add it into the "mailto" string so that when the entered mail is clicked it will open in the users e-mail program with the entered mail preentered. 但是有一件事我无法做到,那就是从用户那里获取电子邮件输入并将其添加到“mailto”字符串中,这样当点击输入的邮件时它将在用户e-中打开输入邮件的邮件程序预先输入。
Here is the code 这是代码
<!DOCTYPE html>
<html>
<head>
<title>HTML JavaScript output on same page</title>
<script type="text/JavaScript">
function showMessage(){
var navn = document.getElementById("navn").value;
display_navn.innerHTML= navn;
var mail = document.getElementById("mail").value;
display_mail.innerHTML= mail;
var tlf = document.getElementById("tlf").value;
display_tlf.innerHTML= tlf;
}
</script>
</head>
<body>
<form>
Navn: <br>
<input type="text" id = "navn"><br>
E-post: <br>
<input type="text" id = "mail"><br>
Mobil: <br>
<input type="text" id = "tlf"><br>
<br>
<br>
<input type="button" onclick="showMessage()" value="submit" />
</form>
<br>
<br>
<span id = "display_navn"></span><br>
<br>
<a href="mailto:" + mail><span id = "display_mail"></a></span><br>
<br>
<span id = "display_tlf"></span>
</body>
</html>
And here is a screenshot of what it looks like. 以下是它的外观截图。
And I want to make it so that when I click on TestEmail the e-mail client I have opens, and the adress is entered already. 我想这样做,以便当我点击TestEmail时,我打开了电子邮件客户端,并且已经输入了地址。 Is this something that can be done? 这是可以做到的吗? If it would be hard to do it on the same site, I could consider having the inputs on a single site, and then open a new site with the generated content when clicking on the submit button if that would be easyer. 如果在同一个站点上很难做到这一点,我可以考虑在单个站点上输入,然后在单击提交按钮时打开包含生成内容的新站点,如果这样更容易。
You just need this extra line of code: 你只需要这个额外的代码行:
display_mail.parentNode.setAttribute("href", "mailto:" + mail);
And don't forget to close the </form>
tag. 并且不要忘记关闭</form>
标记。
Fiddle: http://output.jsbin.com/revahabowe 小提琴: http : //output.jsbin.com/revahabowe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.