Jquery - Linking external .js file not working

a 夏天 提交于 2019-12-01 06:08:13

问题


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 */ });


来源:https://stackoverflow.com/questions/10934234/jquery-linking-external-js-file-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!