简体   繁体   中英

Uncaught ReferenceError:JQuery is not defined

I am learning how to create single page apps from a book called Single Page Web Applications. I have tried the code provided just to build my first simple SPA, but when I launch it the slider isn't working and using developer tools in the browser gives the message "Uncaught ReferenceError: JQuery is not defined". Why does the error happen? The following is the code as it is written in the book. The problem is toward the bottom where I have arrows pointing.

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>SPA Chapter 1 section 1.2.2</title>
  <style type="text/css">
    body {
      width    : 100%;
      height   : 100%;
      overflow : hidden;
      background-color : #777;
    }

    #spa {
      position : absolute;
      top      : 8px;
      left     : 8px;
      bottom   : 8px;
      right    : 8px;
      border-radius    : 8px 8px 0 8px;
      background-color : #fff;
    }

    .spa-slider {
      position : absolute;
      bottom   : 0;
      right    : 2px;
      width    : 300px;
      height   : 16px;
      cursor   : pointer;
      border-radius    : 8px 0 0 0;
      background-color : #F00;
    }
  </style>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

  <script>
  var $ = jQuery
  /*jslint        browser : true, continue : true,
    devel  : true, indent : 2,    maxerr   : 50,
    newcap : true, nomen  : true, plusplus : true,
    regexp : true, sloppy : true, vars : true,
    white  : true
  */
  /*global jQuery */

  // Module /spa/
  // Provides chat slider capability
  //
  var spa = (function ( $ ) {
    // Module scope variables
    var
      // Set constants
      configMap = {
        extended_height : 434,
        extended_title : 'Click to retract',
        retracted_height : 16,
        retracted_title : 'Click to extend',
        template_html : '<div class ="spa-slider"><\/div>'
      },

      // Declare all other module scope variables
      $chatSlider,
      toggleSlider, onClickSlider, initModule;

    // DOM method /toggleSlider/
    // alternates slider height
    //
    toggleSlider = function () {
      var
        slider_height = $chatSlider.height();

      // extend slider if fully retracted
      if ( slider_height === configMap.retracted_height ) {
        $chatSlider
          .animate({ height : configMap.extended_height })
          .attr( 'title', configMap.extended_title );
        return true;
      }

      // retract slider if fully extended
      else if ( slider_height === configMap.extended_height ) {
        $chatSlider
          .animate({ height : configMap.retracted_height })
          .attr( 'title', configMap.retracted_title );
        return true;
      }

      // do not take action if slider is in transition
      return false;
    };

    // Event handler /onClickSlider/
    // receives click event and calls toggleSlider
    //
    onClickSlider = function ( event ) {
      toggleSlider();
      return false;
    };

    // Public method /initModule/
    // sets initial state and provides feature
    //
    initModule = function ( $container ) {

      // render HTML
      $container.html( configMap.template_html );
      $chatSlider = $container.find( '.spa-slider' );

      // initialize slider height and title
      // bind the user click event to the event handler
      $chatSlider
        .attr( 'title', configMap.retracted_title )
        .click( onClickSlider );

      return true;
    };

    return { initModule : initModule }; // <<<<<<<-------
  }( JQuery )); // <<<<<<<--------

  // Start SPA once DOM is ready
  //
  jQuery(document).ready(
    function () { spa.initModule( jQuery('#spa') ); }
  );
  </script>
</head>

<body>
  <div id="spa">
    <div class="spa-slider"></div>
  </div>
</body>
</html>

JavaScript is case sensitive. On the second line you marked with an arrow, change JQuery to jQuery (with a lowercase j ):

}( jQuery ));<<<<<<<--------

Seems like the Jquery script source you're using may be outdated. Try this one:

'<script src="http://code.jquery.com/jquery-latest.min.js"></script>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM