2017-03-03 34 views
0

所以我有我的表起來和運行,但我無法添加一個有效的「malito」鏈接到我的表,並沒有任何好的例子,我可以找。我試過類似如何通過JS文件添加鏈接到表格

"<td>" + <a href = "users[i].email"> + </a> + "</td>" + 

但它不起作用=。

var users = [ 
{first_name: "Kaitlin", last_name: "Burns", age: 23, email: "[email protected]"}, 
{first_name: "Joshua", last_name: "Feir", age: 31, email: "[email protected]"}, 
{first_name: "Stephen", last_name: "Shaw", age: 28, email: "[email protected]"}, 
{first_name: "Timothy", last_name: "McAlpine", age: 37, email: "[email protected]"}, 
{first_name: "Sarah", last_name: "Connor", age: 19, email: "[email protected]"} 
]; 

window.onload = function() 
{ 
var tableContainer = document.querySelector("#outputTable"); 

var table = ""; 
for (var i = 0; i < users.length; i++) 
{ 
    table += "<tr>" + 
       "<td>" + users[i].first_name + "</td>" + 
       "<td>" + users[i].last_name + "</td>" + 
       "<td>" + users[i].age + "</td>" + 
       //Email link here 
      "</tr>"; 
} 
tableContainer.innerHTML += table; 

}; 

HTML文件

<h3> Information </h3> 
     <head> 
      <script src = "js/table.js"></script> 
     </head> 
     <body> 
      <table id = "outputTable" border = "1"> 
      <thead style = "background-color: orangered;"> 
       <tr> 
        <th> First Name </th> 
        <th> Last Name </th> 
        <th> Age </th> 
        <th> Email </th> 
       </tr> 
      </table> 
     </body> 

這是我的表看起來如何

Information 

First Name Last Name Age Email 
Kaitlin  Burns  23 
Joshua  Feir  31 
Stephen  Shaw  28 
Timothy  McAlpine 37 
Sarah  Connor  19 
+0

修正了它,但沒有任何變化 –

回答

0

我寧願叫上自己的或功能自調用,而不是連接到window.onload對於很多其他意想不到的原因。

var users = [ 
 
{first_name: "Kaitlin", last_name: "Burns", age: 23, email: "[email protected]"}, 
 
{first_name: "Joshua", last_name: "Feir", age: 31, email: "[email protected]"}, 
 
{first_name: "Stephen", last_name: "Shaw", age: 28, email: "[email protected]"}, 
 
{first_name: "Timothy", last_name: "McAlpine", age: 37, email: "[email protected]"}, 
 
{first_name: "Sarah", last_name: "Connor", age: 19, email: "[email protected]"} 
 
]; 
 

 
window.onload = function() { 
 
    var tableContainer = document.querySelector("#outputTable"); 
 

 
    var table = ""; 
 
    for (var i = 0; i < users.length; i++) { 
 
    table += "<tr>" + 
 
     "<td>" + users[i].first_name + "</td>" + 
 
     "<td>" + users[i].last_name + "</td>" + 
 
     "<td>" + users[i].age + "</td>" + 
 
     '<td><a href="mailto:' + users[i].email + '"/></td>' + 
 
     "</tr>"; 
 
    } 
 
    tableContainer.innerHTML += table; 
 

 
};
<body> 
 
      <table id = "outputTable" border = "1"> 
 
      <thead style = "background-color: orangered;"> 
 
       <tr> 
 
        <th> First Name </th> 
 
        <th> Last Name </th> 
 
        <th> Age </th> 
 
        <th> Email </th> 
 
       </tr> 
 
      </thead> 
 
      </table> 
 
     </body>

1

您需要添加的mailto:標籤如郵件內部鏈接:

<div> 
    <a href="mailto:[email protected]">send email</a> 
</div> 

小提琴here

0

你的JavaScript代碼行不正確:

"<td><a href=\"mailto:" + users[i].email "\"></a></td>" + 

您正在混合使用JavaScript和HTML,這對初學者程序員來說是非常好的,但卻令人困惑。您的HTML必須使用引號(")完全括在一個字符串中,但HTML 使用引號。那麼如何給字符串添加引號?你通過使用反斜槓(\)「escape」。

反斜槓告訴JavaScript以不同的方式處理下一個字符。例如,\"的意思是「實際上並沒有在這裏結束字符串...我想要一個文字引號」。 \n的意思是「在這裏添加換行符,因爲實際的換行符會破壞我的代碼」。

另外,URL必須是瀏覽器可以識別的東西。 「http:」告訴瀏覽器使用HTTP協議(請參閱What is a URL?)。但是你沒有鏈接到一個網頁,你想「鏈接」到一個電子郵件,所以你需要使用"mailto:" protocol

相關問題