我有以下代碼。我想知道爲什麼它以一種方式工作,而不是另一種。奇怪的jQuery數據表初始化行爲
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Chart</title>
<link href="./css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="./js/jquery-3.1.0.min.js"></script>
<script src="./js/jquery.dataTables.min.js"></script>
<script>
/*
// THIS BLOCK DOES NOT WORK
var tbldata;
$(document).ready
(
function()
{
tbldata = $("#tbldata").DataTable();
}
);
*/
// THIS SINGLE LINE WORKS
var tbldata = $("#tbldata").DataTable();
</script>
</head>
<body>
<div>
<table id="tbldata">
<thead></thead>
<tbody></tbody>
</table>
</div>
</body>
</html>
它給了我下面的錯誤,如果我取消這標誌着我不工作,並排除工作單線上的塊。
jQuery.Deferred exception: Cannot read property 'aDataSort' of undefined TypeError: Cannot read property 'aDataSort' of undefined
at V (http://192.168.33.10:5000/static/js/jquery.dataTables.min.js:65:443)
at va (http://192.168.33.10:5000/static/js/jquery.dataTables.min.js:70:61)
at HTMLTableElement.<anonymous> (http://192.168.33.10:5000/static/js/jquery.dataTables.min.js:91:148)
at Function.each (http://192.168.33.10:5000/static/js/jquery-3.1.0.min.js:2:2815)
at r.each (http://192.168.33.10:5000/static/js/jquery-3.1.0.min.js:2:1003)
at r.m [as dataTable] (http://192.168.33.10:5000/static/js/jquery.dataTables.min.js:82:388)
at r.h.fn.DataTable (http://192.168.33.10:5000/static/js/jquery.dataTables.min.js:166:245)
at HTMLDocument.<anonymous> (http://192.168.33.10:5000/static/test.html:30:45)
at j (http://192.168.33.10:5000/static/js/jquery-3.1.0.min.js:2:29568)
at k (http://192.168.33.10:5000/static/js/jquery-3.1.0.min.js:2:29882) undefined
據我所知,您所評論的區塊必須正常工作,且單行不得起作用。如果它的其他方式是**奇怪** –