作为老师,我可以为您提供一些示例代码,以演示如何使用<li>
、<table>
和复选框(checkbox)来获取外部JSON数据。
- 使用
<li>
列表显示外部JSON数据:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用<li>显示外部JSON数据</title>
<script>
// 创建 XMLHttpRequest 对象
let request = new XMLHttpRequest();
// 发起 GET 请求,获取 JSON 数据
request.open("GET", "data.json", true);
// 监听请求状态变化
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
// 将 JSON 数据解析为 JavaScript 对象
let data = JSON.parse(request.responseText);
// 获取 <ul> 元素
let ul = document.getElementById("jsonList");
// 遍历数据填充列表项
data.forEach(function(item) {
let li = document.createElement('li');
li.innerText = item.name;
ul.appendChild(li);
});
}
}
// 发送请求
request.send();
</script>
</head>
<body>
<ul id="jsonList"></ul>
</body>
</html>
- 使用
<table>
显示外部JSON数据:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用<table>显示外部JSON数据</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
</style>
<script>
// 创建 XMLHttpRequest 对象
let request = new XMLHttpRequest();
// 发起 GET 请求,获取 JSON 数据
request.open("GET", "data.json", true);
// 监听请求状态变化
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
// 将 JSON 数据解析为 JavaScript 对象
let data = JSON.parse(request.responseText);
// 获取 <table> 元素
let table = document.getElementById("jsonTable");
// 创建表头
let thead = table.createTHead();
let headerRow = thead.insertRow();
Object.keys(data[0]).forEach(function(key) {
let th = document.createElement("th");
th.innerText = key;
headerRow.appendChild(th);
});
// 填充表格数据
let tbody = table.createTBody();
data.forEach(function(item) {
let row = tbody.insertRow();
Object.values(item).forEach(function(value) {
let cell = row.insertCell();
cell.innerText = value;
});
});
}
}
// 发送请求
request.send();
</script>
</head>
<body>
<table id="jsonTable"></table>
</body>
</html>
- 使用复选框显示外部JSON数据:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用复选框显示外部JSON数据</title>
<script>
// 创建 XMLHttpRequest 对象
let request = new XMLHttpRequest();
// 发起 GET 请求,获取 JSON 数据
request.open("GET", "
data.json", true);
// 监听请求状态变化
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
// 将 JSON 数据解析为 JavaScript 对象
let data = JSON.parse(request.responseText);
// 获取容器元素
let container = document.getElementById("checkboxContainer");
// 遍历数据创建复选框
data.forEach(function(item) {
let label = document.createElement("label");
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.value = item.name;
label.appendChild(checkbox);
label.appendChild(document.createTextNode(item.name));
container.appendChild(label);
});
}
}
// 发送请求
request.send();
</script>
</head>
<body>
<div id="checkboxContainer"></div>
</body>
</html>
以上是三个示例代码,分别展示了如何使用<li>
、<table>
和复选框来获取外部JSON数据,并根据数据填充相关元素。请确保将data.json
替换为实际的JSON数据文件的路径。
当然,我可以为您提供对应的JSON数据示例。以下是与前面示例代码匹配的JSON数据:
- data.json(用于使用
<li>
显示外部JSON数据):
[
{ "name": "Item 1" },
{ "name": "Item 2" },
{ "name": "Item 3" },
{ "name": "Item 4" }
]
- data.json(用于使用
<table>
显示外部JSON数据):
[
{ "name": "John", "age": 25, "country": "USA" },
{ "name": "Emma", "age": 28, "country": "UK" },
{ "name": "Satoshi", "age": 31, "country": "Japan" },
{ "name": "Sophie", "age": 22, "country": "France" }
]
- data.json(用于使用复选框显示外部JSON数据):
[
{ "name": "Option 1" },
{ "name": "Option 2" },
{ "name": "Option 3" },
{ "name": "Option 4" }
]
请将相应的JSON数据保存为名为data.json
的文件,并将其与相应的HTML文件放置在同一目录下,以使示例代码能够正确加载和解析JSON数据。