2017-03-04 128 views
1

所以我想弄清楚在使用javascript容器時如何將mailto鏈接添加到表格單元格。在表格單元格中添加一個mailto鏈接(HTML5)

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 myRows = ""; 

     for(var i=0; i < users.length; i++){ 
      myRows += "<tr>" + 
         "<td>" + users[i].first_name + "</td>" + 
         "<td>" + users[i].last_name + "</td>" + 
         "<td>" + users[i].age+ "</td>" + 
         "<td>" + users[i].email + "</td>" + 
         "</tr>"; 
     } 

     tableContainer.innerHTML += myRows; 
}; 

所以bascially顯示的代碼後,我都在那裏,我想要做的就是讓「郵件」部分轉成一個mailto鏈接。我該如何解決這個問題?

回答

1

所有你需要做的是:

var myRows = ""; 

    for(var i=0; i < users.length; i++){ 
     myRows += "<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 + "\">"+users[i].email +"</></td>" + 
        "</tr>"; 
    } 

    tableContainer.innerHTML += myRows; 
}; 

我們在這裏做在表中添加標籤<a href="mailto:[email protected]">[email protected]</a>

+0

謝謝!我甚至無法告訴你,我花了多長時間來嘗試解決這個問題,我想我的語法錯了。非常感激 –

相關問題