首页 文章

我的JavaScript在Chrome中不起作用,没有错误,在其他地方有效

提问于
浏览
-1

我的代码似乎适用于除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></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>

1 回答

  • 1

    在您的代码中,您将 bootstrap.min.css 文件包含在 script 标记中 . 其他浏览器可能忽略了但是,Chrome不是't, because it'无效 . 您已经将它正确地包含在文档的头部,所以只需删除该行 .

    此外,如果您使用Chrome开发工具>控制台打开原始代码,您应该看到:

    bootstrap.min.css:5 Uncaught SyntaxError:意外的令牌{

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

相关问题