Jquery - Linking external .js file not working

2020-03-26 05:14发布

问题:

For some reason the external .js file I am linking to isn't working. I am linking to it like so:

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

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

I have tested jquery using a simple inline script to hide a paragraph of text when it is clicked on so the jquery library is present and working.

The jquery.js file is in the same folder as the index.php file that is calling it.

What am I doing wrong?

This is the code I have in the external .js file currently just to test it is working(it isn't):

$("document").ready(function(){

    $("p").click(function(){
        $("p").css("color", "red");

    });


});

回答1:

Problem 1

It looks like jquery.js contains the code you wrote that depends on jQuery.

You need to load jQuery before you try to use it.

Swap the order of the <script> elements.


Problem 2

$("document") will wait for <document> elements to be ready. HTML doesn't have such a thing. Lose the quotes to pass in the document object directly.

Better yet, forget about the explicit call to ready and just

jQuery(function () { /* your function */ });