我的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未捕获语法错误:意外的令牌{

这里有一个使用模拟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>

相关内容

最新更新