简体   繁体   English

JavaScript:getElementByID和mailTo发出

[英]JavaScript: getElementByID and mailTo issue

So I'm trying to add a mailto with javascript but i cannot do this 所以我想用Javascript添加一个mailto,但是我做不到

document.getElementById("email").innerHTML = <a href="mailto:arr1[0].analy1.email?">;

because this produces a couple of errors. 因为这会产生几个错误。 In my javascript code arr1[0].analy1.email actually holds an email address and I can get the email address to show up on my page with 在我的JavaScript代码中,arr1 [0] .analy1.email实际上包含一个电子邮件地址,我可以通过以下方式获取该电子邮件地址以显示在我的页面上

document.getElementById("email").innerHTML = arr1[0].analy1.email;

but I need to know how to make this innerHTML email an actual mailto: link... 但我需要知道如何使这些innerHTML电子邮件成为实际的mailto:链接...

String concatenation: 字符串串联:

document.getElementById("email").innerHTML = '<a href="mailto:' + arr1[0].analy1.email + '">';

Or with the new ECMAScript 6 template strings: 或使用新的ECMAScript 6模板字符串:

document.getElementById('email').innerHTML = `<a href="mailto:${arr1[0].analy1.email}">`

Other than string concatenation as @tymeJV's answer you can alternatively use DOM API to create and insert DOM elements 除了将字符串串联作为@tymeJV的答案外,您还可以使用DOM API创建和插入DOM元素

var email = document.getElementById("email"),
    a = document.createElement('a');
if (email.hasChildNodes()) {
    email.removechild(email.firstChild);
}
a.href = 'mailto:' + arr1[0].analy1.email;
a.textContent = arr1[0].analy1.email; // This will add the actual text to the link

email.appendChild(a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM