数据表不显示数据

我已经像网站提供的那样包含了 CDN,即使复制粘贴了每一行代码,仍然无法显示或显示任何结果。

这是CDN


<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>

这是我的代码


<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>

<body>

<table id="example" class="display">

    <thead>

        <tr>

            <th>Name</th>

            <th>Position</th>

            <th>Salary</th>

        </tr>

    </thead>

</table>

</body>

这是脚本


[

    {

        "name":       "Tiger Nixon",

        "position":   "System Architect",

        "salary":     "$3,120"

    },

    {

        "name":       "Garrett Winters",

        "position":   "Director",

        "salary":     "$5,300"

    }

]

$('#example').DataTable( {

    data: data,

    columns: [

        { data: 'name' },

        { data: 'position' },

        { data: 'salary' }

    ]

} );


梦里花落0921
浏览 129回答 2
2回答

富国沪深

你错过了两件事:JQuery 库变量data_请检查以下解决方案。var data = [{&nbsp; &nbsp; "name": "Tiger Nixon",&nbsp; &nbsp; "position": "System Architect",&nbsp; &nbsp; "salary": "$3,120"&nbsp; },&nbsp; {&nbsp; &nbsp; "name": "Garrett Winters",&nbsp; &nbsp; "position": "Director",&nbsp; &nbsp; "salary": "$5,300"&nbsp; }]$('#example').DataTable({&nbsp; data: data,&nbsp; columns: [{&nbsp; &nbsp; &nbsp; data: 'name'&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; data: 'position'&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; data: 'salary'&nbsp; &nbsp; }&nbsp; ]});<html><link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css"><script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script><script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script><body><table id="example" class="display">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Position</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Salary</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead></table></body></html>

哈士奇WWW

jquery datatables.js 需要jquery。你应该在 jquery.datatables.js 之前添加它。&nbsp;<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.css">&nbsp; &nbsp; &nbsp;<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>&nbsp; &nbsp; &nbsp;<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.js"></script>&nbsp; And you even don't need to define columns in your datatable, datatables does the work itself :&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp;<table id="example" class="display" width="100%"></table>&nbsp; &nbsp; </body>&nbsp; &nbsp; <script>&nbsp; &nbsp; var dataSet = [&nbsp; &nbsp; &nbsp; [ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ],&nbsp; &nbsp; &nbsp; [ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ],&nbsp; &nbsp;&nbsp; &nbsp; ];&nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; $('#example').DataTable( {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data: dataSet,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;columns: [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: "Name" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: "Position" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: "Office" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: "Extn." },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: "Start date" },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { title: "Salary" }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;]&nbsp; &nbsp; &nbsp; } );&nbsp; &nbsp; &nbsp;} );&nbsp; &nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript