gpt4 book ai didi

javascript - HTML 和 JSON 之间交互的脚本帮助

转载 作者:行者123 更新时间:2023-11-27 22:40:13 25 4
gpt4 key购买 nike

寻找有关将 JSON 文件信息传递到 HTML 的脚本的指导。 HTML 将是单个页面,但使用 jQuery Mobile 创建多页面效果。在第一页上,我希望只显示每个人的姓名,然后当您单击该人时,该人的信息将显示在第二页上。我是 JSON 新手,似乎无法全神贯注地管理这个脚本。

HTML 文件:

<!--first page -->
<div data-role="page" id="info-page">
<div data-role="header" data-theme="b">
<h1>Contacts</h1>
</div>
<div data-role="content">
<ul data-role="listview" data-autodividers="true" data-filter="true" data-inset="true" id="prof-list">
<li data-role="list-divider" data-theme="b" role="heading">Names</li>
<!-- List of all names from the JSON file listed here -->
</ul>
</div>
</div>
<!--second page -->
<div data-role="page" id="details-page">
<div data-role="header" data-theme="b"><a href="#" data-rel="back" data-role="button">Go back</a>
<h1>Information</h1>
</div>
<div data-role="list-view">
<!-- Information for person who was clicked on show here -->
</div>
</div>

JSON 文件:(实际文件上还有更多人)

{
"id": 0,
"age": 31,
"name": "Alex Jones",
"email": "alexjones@gmail.com",
"phone": "+1 (845) 575-2978"
}, {
"id": 1,
"age": 31,
"name": "Alice Hollow",
"email": "alicehollow@gmail.com",
"phone": "+1 (829) 454-3806"
},

最佳答案

这是假设您已经将 JSON 作为对象数组,因为我无法从您的部分 JSON 中看出它的格式。这里位于 PLUNKER 中。

片段

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>jQuery Mobile Web App</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script>
</head>

<body>

<div data-role="page" id="page">
<div data-role="header">
<h1>Page One</h1>
</div>
<div data-role="content">
<ul data-role="listview">
<li><a href="#page2">Page Two</a>
</li>
<li><a href="#page3">Page Three</a>
</li>
<li><a href="#page4">Page Four</a>
</li>
</ul>
<ul data-role="listview" data-autodividers="true" data-filter="true" data-inset="true" id="prof-list">
<li data-role="list-divider" data-theme="b" role="heading">Names</li>
<!-- List of all names from the JSON file listed here -->
</ul>
</div>
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>


<div data-role="page" id="page2">
<div data-role="header" data-theme="b"><a href="#" data-rel="back" data-role="button">Go back</a>
<h1>Information</h1>
</div>
<div data-role="content">
<table>
<tr>
<th>Name</th>
<td id="dataName">&nbsp;</td>
</tr>
<tr>
<th>Age</th>
<td id="dataAge">&nbsp;</td>
</tr>
<tr>
<th>Email</th>
<td id="dataEmail">&nbsp;</td>
</tr>
<tr>
<th>Phone</th>
<td id="dataPhone">&nbsp;</td>
</tr>
</table>
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
</div>

<div data-role="page" id="page3">
<div data-role="header">
<h1>Page Three</h1>
</div>
<div data-role="content">
Content
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
</div>

<div data-role="page" id="page4">
<div data-role="header">
<h1>Page Four</h1>
</div>
<div data-role="content">
Content
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
</div>
<script>
var contacts = [{
"id": 0,
"age": 31,
"name": "Alex Jones",
"email": "alexjones@gmail.com",
"phone": "+1 (845) 575-2978"
}, {
"id": 1,
"age": 31,
"name": "Alice Hollow",
"email": "alicehollow@gmail.com",
"phone": "+1 (829) 454-3806"
}, {
"id": 2,
"age": 99999,
"name": "Lazerus",
"email": "What is this EE-mall you speak of?",
"phone": "Fown? I know not of this person."
}];

var listName = document.getElementById("prof-list");


function displayNames(arr) {
var out = "";
var i;
for (i = 0; i < arr.length; i++) {
out += '<li><a href="' + arr[i].name + '" id="' + arr[i].id + '">' + arr[i].name + '</a></li>';
}
listName.innerHTML = out;
listName.addEventListener('click', function(e) {
e.preventDefault();
if (e.target !== e.currentTarget) {
var ID = parseInt(e.target.id, 10);
document.getElementById('dataName').innerHTML = arr[ID].name;
document.getElementById('dataAge').innerHTML = arr[ID].age;
document.getElementById('dataEmail').innerHTML = arr[ID].email;
document.getElementById('dataPhone').innerHTML = arr[ID].phone;
}
}, false);

}

displayNames(contacts);
</script>
</body>

</html>

关于javascript - HTML 和 JSON 之间交互的脚本帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38807592/

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