【发布时间】:2017-11-05 14:15:38
【问题描述】:
我正在尝试使用 jQuery 将 JSON 文件读入动态 HTML 表。具体来说,我正在尝试在 body 类的 div 内创建表。我一直在关注http://www.encodedna.com/jquery/read-json-file-push-data-into-array-and-convert-to-html-table-using-jquery.htm,但是当我查看网页时,没有创建表格。我确定我已将 $.getJSON 指向正确的文件路径。如果您想查看任何特定代码,我将使用它进行编辑。提前致谢。
编辑 - 添加脚本代码
<script type="text/javascript">
$(document).ready(function() {
$.getJSON("/js/na_lcs_results.json", function(data) {
var arrItems = [];
$.each(data, function(index, value) {
arrItems.push(value);
});
//Extract value for table header
var col = [];
for (var i = 0; i < arrItems.length; i++) {
if (col.indexOf(key) === -1) {
col.push(key);
}
}
}
//Create dynamic table
var table = document.createElement("table");
//Create HTML table header using extracted headers
var tr = table.insertRow(-1);
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th");
th.innerHTML = col[i];
tr.appendChild(th);
}
//Add JSON data to table as rows
for (var i = 0; i < arrItems.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
var tabCell = tr.insertCell(-1);
tabCell.innerHTML = arrItems[i][col[j]];
}
}
//Add new table with JSON data to container
var divContainer = document.getElementById("showData");
divContainer.innerHTML = "";
divContainer.appendChild(table);
});
});
Edit - 我要添加到的 Div 类
<div class="NA_LCS">
<h1>Welcome to the NA LCS page</h1>
<div id="showData"></div>
</div>
编辑 - 表格的外部 CSS 页面
table, th, td {
margin: 10px 0;
border: solid 1px #333;
padding: 2px 4px;
font: 15px Abel; }
< script >
$('#home_button').click(function() {
$('.home_screen').fadeIn();
$('#mySidebar,.EU_LCS,.NA_LCS').fadeOut();
});
$('#EU_LCS_button').click(function() {
$('.EU_LCS').fadeIn();
$('#mySidebar,.home_screen,.NA_LCS').fadeOut();
});
$('#NA_LCS_button').click(function() {
$('.NA_LCS').fadeIn();
$('#mySidebar,.home_screen,.EU_LCS').fadeOut();
}); <
/script>
<
script >
function w3_open() {
document.getElementById("mySidebar").style.display = "block";
}
function w3_close() {
document.getElementById("mySidebar").style.display = "none";
} <
/script>
<!-- <script type="text/javascript">
$(document).ready(function() {
$.getJSON("/js/na_lcs_results.json", function(data) {
var arrItems = [];
$.each(data, function(index, value) {
arrItems.push(value);
});
//Extract value for table header
var col = [];
for (var i = 0; i < arrItems.length; i++) {
if (col.indexOf(key) === -1) {
col.push(key);
}
}
}
//Create dynamic table
var table = document.createElement("table");
//Create HTML table header using extracted headers
var tr = table.insertRow(-1);
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th");
th.innerHTML = col[i];
tr.appendChild(th);
}
//Add JSON data to table as rows
for (var i = 0; i < arrItems.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
var tabCell = tr.insertCell(-1);
tabCell.innerHTML = arrItems[i][col[j]];
}
}
//Add new table with JSON data to container
var divContainer = document.getElementById("showData"); divContainer.innerHTML = ""; divContainer.appendChild(table);
});
}); -->
<
/script>
<
script >
$(document).ready(function() {
$('#tabs').tabs();
$("accordion1").accordion();
$("#accordion2").accordion();
}); <
/script>
#mySidebar img {
width: 32px;
height: 32px;
}
.home_screen {
margin: 70px;
}
.NA_LCS {
display: none;
margin: 70px;
}
.EU_LCS {
display: none;
margin: 70px;
}
body {
background-color: #8c8c8c;
}
#banner {
background-image: url('../img/lol_universe_bg.jpg');
background-size: cover;
background-position: center;
width: 800;
height: 300px;
}
h1 {
font-family: 'Abel';
font-size: 48px;
text-align: center;
}
table,
th,
td {
margin: 10px 0;
border: solid 1px #333;
padding: 2px 4px;
font: 15px Abel;
}
th {
font-weight: bold:
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LoL Universe</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Abel">
<link rel="stylesheet" type="text/css" href="css/mystylesheet.css">
</head>
<body>
<div class="w3-sidebar w3-bar-block w3-black w3-animate-left" style="display:none" id="mySidebar">
<button class="w3-bar-item w3-button w3-large" onclick="w3_close()">Close ×</button>
<a href="#" class="w3-bar-item w3-button" id="home_button"><img src="img/icon_home.png"></a>
<a href="#" class="w3-bar-item w3-button" id="EU_LCS_button"><img src="img/EU_LCS_logo.png"></a>
<a href="#" class="w3-bar-item w3-button" id="NA_LCS_button"><img src="img/NA_LCS_logo.png"></a>
</div>
<div class="nav_button">
<button class="w3-button w3-white w3-xxlarge" onclick="w3_open()">☰</button>
</div>
<div id="banner"></div>
<div class="home_screen">
<h1>Welcome to LoL Universe</h1>
</div>
<div class="EU_LCS">
<h1>Welcome to the EU LCS page</h1>
</div>
<div class="NA_LCS">
<h1>Welcome to the NA LCS page</h1>
<!-- <div id="showData"></div> -->
</div>
</body>
</html>
【问题讨论】:
-
在您的 sn-p 中,您正在引用本地文件。例如:
<link rel="stylesheet" type="text/css" href="css/mystylesheet.css">,或此处:$.getJSON("/js/na_lcs_results.json", function(data) {,或此处:background-image: url('../img/lol_universe_bg.jpg');。并且,您将<script>标签粘贴到了 sn-p 中的 JS 部分。编辑它并使其具有可读性和意义。 -
抱歉,我以前从未使用过 Snippet。引用的本地文件是我在 Snippet 中附加的 CSS 表、一个 JSON 文件和一个背景图像。我不认为它们是必需的,因为我只需要知道为什么它不会为我将 JSON 数据读入表中。