在表格单元格中添加mailto链接(HTML5)

时间:2017-03-04 18:29:44

标签: javascript html containers

所以我想弄清楚如何在使用javascript容器时添加mailto链接到表格单元格。

var users = [ {first_name: "Kaitlin", last_name: "Burns", age: 23, email: "kburns99753@usermail.com"}, {first_name: "Joshua", last_name: "Feir", age: 31, email: "josh319726@usermail.com"}, {first_name: "Stephen", last_name: "Shaw", age: 28, email: "steve.shaw47628@usermail.com"}, {first_name: "Timothy", last_name: "McAlpine", age: 37, email: "Timbo72469@usermail.com"},    {first_name: "Sarah", last_name: "Connor", age: 19, email:"SarahC6320@usermail.com"}
];

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; 
};

所以在显示我在那里的代码之后,我想要做的就是制作&#34;电子邮件&#34;部分变成了mailto链接。我怎么能这样做?

1 个答案:

答案 0 :(得分: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:themail@email.com">themail@email.com</a>