gpt4 book ai didi

javascript - 使用 json 文件创建购物 list

转载 作者:行者123 更新时间:2023-11-30 12:54:50 26 4
gpt4 key购买 nike

好的,我有我的 json 文件:

[
{"task":"get milk","who":"Scott","dueDate":"2013-05-19","done":false},
{"task":"get broccoli","who":"Elisabeth","dueDate":"2013-05-21","done":false},
{"task":"get garlic","who":"Trish","dueDate":"2013-05-30","done":false},
{"task":"get eggs","who":"Josh","dueDate":"2013-05-15","done":true}
]

和我的 html/jquery 文件:

<!DOCTYPE html>
<html>
<head>
<title>To do List</title>
<meta charset="utf-8">
<style>
</style>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script>
$("document").ready(function() {
$.getJSON("todo.json", function(data) {
console.log("Data loaded successfully");
});
});
</script>
</head>
<body>
<div id="list">
<ul class="list-items">
</ul>
</div>
</body>
</html>

我想做的是获取每个 json 数组并用它创建一个待办事项列表;但是,我在显示数组时遇到了困难,因此我可以将它们添加到列表项中

最佳答案

这里和jQuery差不多吧

HTML:

<ul class="list-items">
</ul>

jQuery:

var data = [
{"task":"get milk","who":"Scott","dueDate":"2013-05-19","done":false},
{"task":"get broccoli","who":"Elisabeth","dueDate":"2013-05-21","done":false},
{"task":"get garlic","who":"Trish","dueDate":"2013-05-30","done":false},
{"task":"get eggs","who":"Josh","dueDate":"2013-05-15","done":true}
];

var ul = $('.list-items');
$.each(data, function(i, elem) {
var text = 'Task: ' + elem.task + ', Name: ' + elem.who + ', Date: ' + elem.dueDate;
ul.append('<li>' + text + '</li>');
});

JSFiddle

关于javascript - 使用 json 文件创建购物 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19577784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com