未捕获的ReferenceError:未在DataTable上定义$

时间:2018-11-15 04:36:17

标签: javascript jquery ajax datatables

所以我在控制台上看到一个错误

Uncaught ReferenceError: $ is not defined
    at 2e9ecdab-e88c-11e8-95f0-02fba84c29d0:1934

当我深入研究代码时,问题出在此代码中:

$('#cashFlow').DataTable({ //ReferenceError: $ is not defined 
dom: 'Bfrtip',
buttons: [
  {
    extend: 'excel',
    text: 'Export to Excel',
    className: 'btn btn-default',
    exportOptions: {
      columns: 'th:not(:last-child)'
    }
  }],
'paging'      : true,
'searching'   : true,
'ordering'    : true,
"columns" : [
  {"width" : "5%"},
  {"width" : "10%"},
  {"width" : "25%"},
  {"width" : "5%"},
  {"width" : "15%"},
  {"width" : "3%"},
  {"width" : "15%"},
  {"width" : "2%"}
]});

$('#cashFlow')表已存在,并且已经在此代码的顶部。

<table id="cashFlow" class="table table-striped table-bordered">
<thead>
<tr>
  // ...
  <th>{{__('grabLog.action')}}</th>
</tr>
</thead>
@if(isset($cashflow))
  @foreach($cashflow as $index=>$f)
    <tr>
      // ...
      <td>
        <button class="btn btn-danger btn-cf-delete" bank_account_id="{{$f->bank_account_id}}"
                trans_date="{{$f->trans_date}}"
                description="{{$f->description}}" cab="{{$f->cab}}" amount="{{$f->amount}}"
                type="{{$f->type}}" balance="{{$f->balance}}" skey="{{$skey}}"><i class="fa fa-trash"></i>
        </button>
      </td>
    </tr>
  @endforeach
@endif

我已经在该论坛中搜索了这个问题,所有答案都将jquery.min.js放在了最前面。我已经做到了,但是仍然有同样的问题。

<script src="{{asset('AdminLte/js')}}/jquery/dist/jquery.min.js"></script>
<script src="{{asset('AdminLte/js')}}/jquery/jquery-3.3.1.js"></script>
<script src="{{asset('AdminLte/js')}}/bootstrap/bootstrap.min.js"></script>
<script src="{{asset('AdminLte/js')}}/datatables.net/jquery.dataTables.min.js"></script>
<script src="{{asset('AdminLte/js')}}/datatables.net-bs/dataTables.bootstrap.min.js"></script>
<script src="{{asset('AdminLte/js')}}/datatables.net/dataTables.buttons.min.js"></script>
<script src="{{asset('AdminLte/js')}}/datatables.net/buttons.flash.min.js"></script>
<script src="{{asset('AdminLte/js')}}/ajax/jszip.min.js"></script>
<script src="{{asset('AdminLte/js')}}/ajax/pdfmake.min.js"></script>
<script src="{{asset('AdminLte/js')}}/ajax/vfs_fonts.js"></script>
<script src="{{asset('AdminLte/js')}}/datatables.net/buttons.html5.min.js"></script>
<script src="{{asset('AdminLte/js')}}/datatables.net/buttons.print.min.js"></script>
<script src="{{asset('AdminLte/js')}}/jquery-slimscroll/jquery.slimscroll.min.js"></script>
//there's another javascripts I put in here but didn't show to you because it doesn't related with this question.
<script>
  $.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
  });
</script>

1 个答案:

答案 0 :(得分:2)

您需要在页面底部加载脚本-防止阻止HTML的加载/呈现。

请注意,唯一应放在头部的js函数是呈现内容所直接需要的那些js函数,例如对数据源API的调用。所有页面内功能都应在页面HTML / CSS之后加载。

然后将jquery函数包装在$(document).ready({})包装器中,以确保DOM准备好与这些函数进行交互。

示例页面结构

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <link rel="shortcut icon" href="favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="/css/bootstrap.min.css">
  <link rel="stylesheet" href="/css/font-awesome.min.css">
</head>

<body>
  <script src="/js/jquery.min.js"></script>
  <script src="/js/bootstrap.min.js"></script>
  <script>

  $(document).ready(function(){
  .... put code here ...
  })
 </script>
</body>
</html>