我有一个 json 数据,我有一个 html 表,我想将 json 数据添加到表中 这就是我尝试获取 json 数据的方式,我使用追加来添加表数据,因为我是 json 解析的新手,我尽了最大努力,任何人都可以帮忙...
我有一个 json 数据,我有一个 html 表,我想将 json 数据添加到表中 这就是我尝试获取 json 数据的方式,我使用追加来添加表数据,因为我是 json 解析的新手,我尽了最大努力,任何人都可以帮忙...
<!DOCTYPE html>
<html>
<head>
<title>JSON Demo</title>
<style>
table,
th,
td {
border: 1px solid black;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function StudentDetails() {
var stu = [{
"stuname": "anbu",
"studep": "cs"
},
{
"stuname": "raj",
"studep": "Maths"
},
{
"stuname": "mani",
"studep": "science"
}
]
var tr = "";
for (var i = 0; i < stu.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + stu.stuname + "</td>");
tr.append("<td>" + stu.studep + "</td>");
$('#student').append(tr);
}
}
</script>
</head>
<body>
<input type="button" onClick="StudentDetails()" value="Student Table" />
<div>
<table id="student">
<thead>
<tr>
<th>Student Name</th>
<th>Student Department</th>
</tr>
<thead>
<tbody></tbody>
</table>
</div>
<div id="showData"></div>
</body>
</html>
最佳答案
将Array的索引添加到stu中
tr.append("<td>" + stu[i].stuname + "</td>");
tr.append("<td>" + stu[i].studep + "</td>");
关于javascript - 将 JSON 数据添加到 Html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50870069/