javascript - 我的 JavaScript 不能在 Chrome 中运行,没有错误,在其他地方也能运行
问题描述
我下面的代码似乎适用于除 Chrome 之外的所有浏览器。我没有收到任何错误,但我也没有看到我期望的表格。任何有助于使其在 Chrome 中工作的帮助将不胜感激!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Ofensas</title>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<p><br/><br></p>
<div class="container">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Descrição</th>
<th>Número</th>
<th>ID</th>
</tr>
</thead>
</table>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" ></script>
<script>
$.getJSON("exemplo.json",function(data){
var items = [];
$.each(data, function(key, val){
items.push("<tr>");
items.push("<td name='" + key + "'>" + val.offense_name +"</td>");
items.push("<td nuemro='" + key + "'>" + val.count + "</td>");
items.push("<td name='" + key + "'>" + val.offense_id + "</td>");
items.push("</tr>");
});
$("<tbody/>", {html: items.join("")}).appendTo("table");
});
</script>
</body>
</html>
解决方案
在您的代码中,您将bootstrap.min.css
文件包含在script
标签中。其他浏览器可能会忽略这一点;但是,Chrome 不是,因为它无效。您已经将它正确地包含在文档的开头,所以只需删除该行。
此外,如果您使用 Chrome 开发工具 > 控制台打开原始代码,您应该会看到:
bootstrap.min.css:5 Uncaught SyntaxError: Unexpected token {
这是一个使用模拟 api 响应来完成您正在做的事情的基本示例。
const apiResponse = {
data: [
{ "count": 42, "offense_id": 42, "offense_name": "Bird Flu" },
{ "count": 56, "offense_id": 56, "offense_name": "Volvulus" }
]
}
var items = [];
$.each(apiResponse.data, function(key, val) {
items.push("<tr>");
items.push("<td name='" + key + "'>" + val.offense_name + "</td>");
items.push("<td nuemro='" + key + "'>" + val.count + "</td>");
items.push("<td name='" + key + "'>" + val.offense_id + "</td>");
items.push("</tr>");
});
$("<tbody/>", {html: items.join("")}).appendTo("table");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Descrição</th>
<th>Número</th>
<th>ID</th>
</tr>
</thead>
</table>
</div>
推荐阅读
- sql-server - 更快地更新多行 SQL Server
- node.js - DynamoDB - 从多个主键(分区键+排序键/哈希键+范围键)获取最新数据
- azure-devops - 为什么 AzureRmWebAppDeploymen 要求我订阅,然后尝试将其用作服务连接?
- c# - CEF 线程问题 - 最简单的修复?
- python - 创建一个可以使用 Python 3 和 tkinter 打开/关闭相机图像的 GUI
- python - 如何导入 Maya Python API?
- python - OpenCV Watermark保持纵横比不给出相同的输入/输出结果
- html - CSS如何使这个div居中
- excel - Excel 对 Index - match 和 sumifs 组合的帮助?
- sql-server - 如何使用spring框架将LDAP数据添加到关系数据库表中的查询数据中