未捕获的ReferenceError:$ jQuery中未定义错误

use*_*565 9 html javascript file-io jquery

我有这个代码jQuery:(文件名是javascript.js ...之前我使用的是JavaScript ...)

$(document).ready(function() {
 $("#readFile").click(function() {
    $.get('test.txt', function(data) {
      $("#bottom_pane_options").html(data); // #bottom_pane_options is the div I want the data to go
    }, 'text');
 });
});
Run Code Online (Sandbox Code Playgroud)

...这在HTML中:

<!DOCTYPE html>
<html>
<head>
    <title>Culminating</title>
    <link href="style.css" rel="stylesheet" type="text/css">

    <script type="text/javascript" src="./javascript.js"></script>
    <script
        src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCJnj2nWoM86eU8Bq2G4lSNz3udIkZT4YY&sensor=false">
    </script>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>


    <script>
        function initialize()
        {
        var mapProp = {
          center:new google.maps.LatLng(50.569283,-84.378433),
          zoom:5,
          mapTypeId:google.maps.MapTypeId.TERRAIN
          };
        var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
        }

        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>

<body>
    <div class="content">
        <div id="googleMap"></div>
        <div id="right_pane_results">hi</div>
        <div id="bottom_pane_options">
            <button id="readFile">Read File</button>
        </div>
    </div>
</body>
Run Code Online (Sandbox Code Playgroud)

当我检查控制台时,我得到了第一行没有定义的Uncaught ReferenceError说法$.我假设它指的是第一行的第一个字符.我从网站上获得了这个代码,我是新手,jQuery所以我不确定这里出了什么问题.

有什么建议?

j08*_*691 41

更改包含脚本的顺序(首先是jQuery):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript" src="./javascript.js"></script>
<script
    src="http://maps.googleapis.com/maps/api/js?key=YOUR_APIKEY&sensor=false">
</script>
Run Code Online (Sandbox Code Playgroud)


Sar*_*ana 7

首先包含jQuery文件:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
 <script type="text/javascript" src="./javascript.js"></script>
    <script
        src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCJnj2nWoM86eU8Bq2G4lSNz3udIkZT4YY&sensor=false">
    </script>
Run Code Online (Sandbox Code Playgroud)


MMM*_*MMM 7

脚本按照您在HTML中定义它们的顺序加载.

因此,如果您首先加载:

<script type="text/javascript" src="./javascript.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后先不加载jQuery $ is not defined.

您需要先加载jQuery才能使用它.

出于性能原因,我还建议将脚本放在HTML的底部.