简体   繁体   English

如何使用JavaScript在表格单元格中写入电子邮件地址?

[英]How can I write an email address inside a table cell with JavaScript?

I am currently trying to get my code to display the email "cadler@mpl.gov" with javascript however my code is not displaying the text. 我目前正在尝试获取我的代码以使用javascript显示电子邮件“ cadler@mpl.gov”,但是我的代码未显示文本。 Any idea what could be causing it? 知道是什么原因造成的吗?

 var userName = "cadler"; var emServer = "mpl.gov"; var emLink = username + "@" + emServer; document.write("<a href='mailto:" + emLink + "'>"); document.write(emLink); document.write("</a>"); 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td></td> </table> 

The Javascript should insert the email address in the empty table cell. Javascript应该在空表单元格中插入电子邮件地址。

Firstly, as others have mentioned, it should be userName , not username . 首先,正如其他人提到的那样,它应该是userName ,而不是username

Secondly, don't use document.write. 其次,不要使用document.write。 Use Element.innerHTML or create a textnode to append to the element, combined with editing the href attribute. 使用Element.innerHTML或创建一个textnode附加到元素上,并结合编辑href属性。

The code becomes: 代码变为:

 var userName = "cadler", emServer = "mpl.gov", emLink = userName + "@" + emServer; document.getElementsByClassName("email")[0].innerHTML = "<a href='mailto:" + emLink + "'>"+emLink+"</a>"; 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td class="email"></td> </tr> </table> 

 var userName = "cadler", emServer = "mpl.gov", emLink = userName + "@" + emServer, emTextNode = document.createTextNode(emLink), elem = document.getElementsByClassName("email")[0]; elem.setAttribute("href", "mailto:" + emLink); elem.appendChild(emTextNode); 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td class="email"></td> </tr> </table> 

As others mentioned, do not use document.write. 如其他人所述,请勿使用document.write。 That said, if you have to use document.write, then put the code inside the td : 就是说,如果您必须使用document.write,则将代码放在td

<td class="email">
    <script>
        var userName = "cadler",
            emServer = "mpl.gov",
            emLink = userName + "@" + emServer;

        document.write("<a href='mailto:" + emLink + "'>");
        document.write(emLink);
        document.write("</a>");
    </script>
</td>

You had a typo: username instead of userName at the third line. 您在第三行username错了: username而不是userName Also you were not adding the email to the table's column. 另外,您没有将电子邮件添加到表格的列中。 You can do this by using a span element inside the td : 您可以通过在td内使用span元素来做到这一点:

  var userName = "cadler"; var emServer = "mpl.gov"; var emLink = userName + "@" + emServer; var email = document.getElementById("email"); email.innerHTML = "<a href='mailto:" + emLink + "'>" + emLink + "</a>"; 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td><span id="email"></span></td> </tr> </table> 

The first problem I see, is that you first define your variable userName and later refer to it as username . 我看到的第一个问题是,您首先定义变量userName ,然后将其称为username You forgot to capitalize the letter n ! 您忘了将字母n大写!

Fix that, and the email address will be displayed AFTER the table : 修复此问题,该电子邮件地址将在表格后显示:

 var userName = "cadler"; var emServer = "mpl.gov"; var emLink = userName + "@" + emServer; document.write("<a href='mailto:" + emLink + "'>"); document.write(emLink); document.write("</a>"); 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td></td> </table> 

To display it in the empty cell, you need to make a few more adjustments : 要将其显示在空白单元格中,您需要进行一些其他调整:

 var userName = "cadler"; var emServer = "mpl.gov"; var emLink = userName + "@" + emServer; var email = "<a href='mailto:" + emLink + "'>" + emLink + "</a>"; document.querySelector('td:last-child').innerHTML = email; 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td></td> </table> 

document.querySelector('td:last-child') selects the last td element. document.querySelector('td:last-child')选择最后一个td元素。 Changing its innerHTML allows you to put a value in that element. 更改其innerHTML允许您在该元素中添加一个值。

Anyway, you should never ever ever use document.write . 无论如何,永远不要使用document.write It's been considered a bad practice for many years now. 多年来,人们一直认为这是一种不良做法。 Instead, always use something like document.getElementById or document.querySelector and change the innerHTML of that element. 相反,请始终使用诸如document.getElementByIddocument.querySelector类的东西并更改该元素的innerHTML

there is a type in your variable name userName. 您的变量名userName中有一种类型。 You can set this value to empty cell. 您可以将此值设置为空单元格。 You have defined no id or class, selecting it will not be very clean, you can do something like this: 您没有定义id或class,选择它不会很干净,您可以执行以下操作:

  var userName = "cadler"; var emServer = "mpl.gov"; var emLink = userName + "@" + emServer; document.write("<a href='mailto:" + emLink + "'>"); document.write(emLink); document.write("</a>"); var table = document.getElementsByTagName('table')[0] var row = table.getElementsByTagName('tr')[1] row.getElementsByTagName('td')[2].innerHTML = "<a href='mailto:" + emLink + "'>" + emLink + "</a>"; 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td> </td> 

All of the other answers use innerHTML to insert the <a> element into the DOM. 所有其他答案都使用innerHTML<a>元素插入DOM。 However, there are several reasons not to use it. 但是,有几个原因不使用它。

The following snippet provides a solution without innerHTML : 以下代码片段提供了一个没有innerHTML的解决方案:

 var userName = "cadler"; var emServer = "mpl.gov"; var emLink = userName + "@" + emServer; // create a new <a> element and set the content and href attribute var linkElement = document.createElement("a"); linkElement.href = "mailto:" + emLink; linkElement.textContent = emLink; // find the element with the ID "email-cell" var td = document.getElementById("email-cell"); td.appendChild(linkElement); 
 <table border="1" cellpadding="5" cellspacing="0"> <tr> <th>Name</th> <th>Phone</th> <th>E-Mail</th> </tr> <tr> <td>Catherine Adler<br />Library Director</td> <td>555-3100</td> <td id="email-cell"></td> <!-- this cell will contain the link --> </tr> </table> 

Read about document.createElement and Node.prototype.appendChild on MDN. 阅读有关MDN上的document.createElementNode.prototype.appendChild信息。

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

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