首页 > 解决方案 > 我的 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>

标签: javascripthtmlcsscross-browser

解决方案


在您的代码中,您将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>


推荐阅读