Uncaught ReferenceError: $ is not defined error in jQuery

前端 未结 4 2019
无人共我
无人共我 2020-11-29 10:49

I have this code in jQuery: (the file name is javascript.js ...I was using JavaScript before...)

$(document).ready(function() {
 $(\"#readFile\"         


        
相关标签:
4条回答
  • 2020-11-29 11:04

    Scripts are loaded in the order you have defined them in the HTML.

    Therefore if you first load:

    <script type="text/javascript" src="./javascript.js"></script>
    

    without loading jQuery first, then $ is not defined.

    You need to first load jQuery so that you can use it.

    I would also recommend placing your scripts at the bottom of your HTML for performance reasons.

    0 讨论(0)
  • 2020-11-29 11:10

    The MVC 5 stock install puts javascript references in the _Layout.cshtml file that is shared in all pages. So the javascript files were below the main content and document.ready function where all my $'s were.

    BOTTOM PART OF _Layout.cshtml:

        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
            </footer>
        </div>
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    
    </body>
    </html>
    

    I moved them above the @RenderBody() and all was fine.

        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    
        <div class="container body-content">
            @RenderBody()
            <hr />
            <footer>
                <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
            </footer>
        </div>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 11:13

    Include the jQuery file first:

     <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>
    
    0 讨论(0)
  • 2020-11-29 11:16

    Change the order you're including your scripts (jQuery first):

    <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>
    
    0 讨论(0)
提交回复
热议问题