我有以下代码。我想知道为什么它以一种方式工作,而不是另一种。奇怪的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
据我所知,您所评论的区块必须正常工作,且单行不得起作用。如果它的其他方式是**奇怪** –