使json数据成为html中的可点击链接

时间:2016-11-28 20:38:42

标签: html json hyperlink styles

我有json数据显示在html页面javascript上,我想知道是否有办法让这些信息像链接一样可点击。任何人都知道如何做到这一点?

这是我的json,我想从json文件中获取每个名称并使其可点击。



{
    "example": [
        {
          "name": "Dr. Sammie Boyer",
          "email": "Lavonne.Kiehn@hotmail.com"
        },
        {
          "name": "Eladio Beier",
          "email": "Lavonne.Kiehn@hotmail.com"
        },
        {
          "name": "Hilton Borer",
          "email": "Reva.Goyette@yahoo.com"
        }
    ]
}




我试过的代码



$(document).ready(function() {


    $.getJSON('example.json', function(data) {
         var output = '';
         $.each(data.name, function(key, value) {
                output += '<a href=' + value.name + '</a>';
        });
       
        $('#user').html(output);
    });
});
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

试试这个(更新) - 在循环时设置href的值

$.getJSON('example.json', function(data) {
      var output = '';
      $.each(data.name, function(key, value) {
            output += '<a href="emailto:' + value.email + '">'  + value.name + '</a>';
    });

    $('#user').html(output);
});

答案 1 :(得分:0)

只需将emailto替换为mailto即可

$.getJSON('example.json', function(data) {
      var output = '';
      $.each(data.name, function(key, value) {
            output += '<a href="mailto:' + value.email + '">'  + value.name + '</a>';
    });

    $('#user').html(output);
});