JQuery:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(function(){
$(':input:eq(0)').click(function(){
$.ajax({
url: 'data.json',
type: 'get',
dataType: 'json',
success: function(datas){
var tbhtml = "<tr><th>姓名</th><th>年龄</th><th>性别</th></tr>";
for(var i = 0;i<datas.length;i++){
tbhtml+="<tr><td>"+datas[i].name+"</td><td>"+datas[i].age+"</td><td>"+datas[i].sex+"</td></tr>";
}
$('table:eq(0)').html(tbhtml);
}
})
})
})
</script>
HTML:
<center>
<table border="1px solid">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</table>
<input type="button" value="加载数据" />
</center>
Json:
[
{
"name": "zhangsan",
"age": 18,
"sex": "man"
},
{
"name": "lisi",
"age": 17,
"sex": "man"
},
{
"name": "wangwu",
"age": 16,
"sex": "man"
}
]
个人实现:
<!-- 用jquery实现数据的显示 -->
<script>
$(function() {
$.ajax({
url: "http://www.test.test/vue学习/vue第3天/mysql.php",
dataType: "json",
success: function(data) {
console.log(data);
var tbhtml = "<table border='1px' cellspacing='0'><tr><th>id:</th><th>user_id</th></tr>";
for(var i = 0;i<data.length;i++){
tbhtml += "<tr><td>"+ data[i].id+"</td><td class='user_id'>"+data[i].user_id+"</td></tr>";
}
tbhtml += "</table>";
$("#project_list").html(tbhtml);
// }
}
});
});
</script>
转载地址:https://blog.csdn.net/xiaohongit/article/details/83855657