Javascript Jquery - 链接外部 .js 文件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10934234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery - Linking external .js file not working
提问by crmepham
For some reason the external .js file I am linking to isn't working. I am linking to it like so:
由于某种原因,我链接到的外部 .js 文件无法正常工作。我像这样链接到它:
<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.
我已经使用一个简单的内联脚本测试了 jquery,当它被点击时隐藏一段文本,因此 jquery 库存在并正常工作。
The jquery.js file is in the same folder as the index.php file that is calling it.
jquery.js 文件与调用它的 index.php 文件位于同一文件夹中。
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):
这是我目前在外部 .js 文件中的代码,只是为了测试它是否正常工作(不是):
$("document").ready(function(){
$("p").click(function(){
$("p").css("color", "red");
});
});
回答by Quentin
Problem 1
问题一
It looks like jquery.js contains the code you wrote that dependson jQuery.
看起来 jquery.js 包含您编写的依赖于 jQuery的代码。
You need to load jQuery beforeyou try to use it.
在尝试使用它之前,您需要加载 jQuery 。
Swap the order of the <script>
elements.
交换<script>
元素的顺序。
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.
$("document")
将等待<document>
元素准备就绪。HTML 没有这样的东西。去掉引号document
直接传入对象。
Better yet, forget about the explicit call to ready
and just
更好的是,忘记显式调用ready
and just
jQuery(function () { /* your function */ });