jQuery-链接外部.js文件不起作用 [英] Jquery - Linking external .js file not working
问题描述
由于某种原因,我链接到的外部.js文件无法正常工作.我这样链接到它:
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>
我已经使用简单的内联脚本对jquery进行了测试,以使其在单击时隐藏一段文本,从而使jquery库存在并且可以正常工作.
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.
jquery.js文件与调用它的index.php文件位于同一文件夹中.
The jquery.js file is in the same folder as the index.php file that is calling it.
我在做什么错了?
这是我目前在外部.js文件中的代码,目的只是为了测试它是否正常工作(不是):
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
jquery.js似乎包含您编写的依赖依赖jQuery的代码.
It looks like jquery.js contains the code you wrote that depends on jQuery.
您需要先加载jQuery,然后再尝试使用它.
You need to load jQuery before you try to use it.
交换<script>
元素的顺序.
问题2
$("document")
将等待<document>
元素准备就绪. HTML没有这样的东西.丢失引号直接传递到document
对象中.
$("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.
更好的是,忘记显式调用ready
和公正
Better yet, forget about the explicit call to ready
and just
jQuery(function () { /* your function */ });
这篇关于jQuery-链接外部.js文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!