我知道这听起来很愚蠢而且毫无意义,但是我正在尝试创建一个网站,该网站从输入的我的工作场所的姓氏和名字中查找电子邮件地址。它仅具有2个文本输入字段,一个用于初始字段,一个用于姓氏。我的JavaScript将这些与电子邮件的布局结合在一起,并将其设置为变量“ email”。如何使用“ mailto:”将其设置为按钮的href标签?我尝试使用onmouseover="this.href = 'mailto:'+email;"
它只是返回undefined;
function buildEmail() {
var initial = document.getElementById("initial").value;
var surname = document.getElementById("surname").value;
var email = initial + surname + "@[emaildomain.com]"
}
<h1>Enter the first initial</h1>
<br>
<input type="text" id="initial" />
<br>
<h2>Enter the surname</h2>
<br>
<input type="text" id="surname" />
<br>
<button>
<a onClick=myFunction() onmouseover="this.href = 'mailto:'+email">Click to send email</a>
</button>
您遇到的问题是您有未调用的buildEmail。事实是,变量email在被调用时会作用于函数,因此它不是全局的。
只需调用onmousedown函数并设置href,以便单击将触发mailto。
function buildEmail(event) {
var initial = document.getElementById("initial").value;
var surname = document.getElementById("surname").value;
var email = initial + surname + "@example.com"
event.target.href = "mailto:" + email;
}
<center><h1>Enter the first initial</h1>
<br>
<input type="text" id="initial" />
<br>
<h2>Enter the surname</h2>
<br>
<input type="text" id="surname" />
<br>
<button><a onmousedown="buildEmail(event)">Click to send email</a></button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句