customScript.js:1未捕获的ReferenceError:$未定义

时间:2017-07-23 19:57:33

标签: javascript

<head>
   <script type="text/javascript" src="customScript.js"></script>
    <link href="dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="docs/assets/js/vendor/jquery.min.js"></script>
    <script src="dist/js/bootstrap.min.js"></script>

    <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 -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="img/facebookicn.png">

    <title>Facbook</title>


    <link href="dist/css/jumbotron.css" rel="stylesheet">
    <link href="dist/css/custom.css" rel="stylesheet">


</head>

//这是在coustom.js文件中

$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();
});

//这是html

    <div class="col-md-6 txtfeild ">
  <a href="#" data-toggle="tooltip" data-placement="left" title="Hooray!"><input id="firstName" class="form-control" placeholder="First Name"  type="text" onblur="validateName()" ></a>
       </div>

我收到这样的错误 customScript.js:1未捕获的ReferenceError:$未定义 在customScript.js:1尝试了很多方法,但无法解决此错误

3 个答案:

答案 0 :(得分:2)

导入customScript脚本之前,jQuery会触发。将导入行移到customScript导入上方。

<script src="docs/assets/js/vendor/jquery.min.js"></script>
<script type="text/javascript" src="customScript.js"></script>
<link href="dist/css/bootstrap.min.css" rel="stylesheet">

答案 1 :(得分:1)

加载customScript.js后加载jQuery,因此变量$未定义。 加载jQuery后必须插入脚本。

<head>
    <link href="dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="docs/assets/js/vendor/jquery.min.js"></script>
    <script src="dist/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="customScript.js"></script>

    <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 -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="img/facebookicn.png">

    <title>Facbook</title>


    <link href="dist/css/jumbotron.css" rel="stylesheet">
    <link href="dist/css/custom.css" rel="stylesheet">

</head>

答案 2 :(得分:0)

为了在脚本中使用Jquery,首先需要加载它

<script


src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>

customeScript之前添加此行以通过cdn添加jquery,或者如果已下载,则设置jquery文件的路径