jQuery $(document).ready(function() 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6341191/
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
$(document).ready(function() is not working
提问by Romi
I am using Jquery for getting a json object from a solr server. When I run my html file with Tomcat it is runns fine but when I embed it with my project which is running on weblogic it gets this error: (debugging done through firebug)
我正在使用 Jquery 从 solr 服务器获取 json 对象。当我用 Tomcat 运行我的 html 文件时,它运行良好,但是当我将它嵌入到我在 weblogic 上运行的项目中时,它会收到此错误:(通过 firebug 完成调试)
$ is not defined
$(document).ready(function(){
Why do I get this error when I embed it in my project?
当我将它嵌入到我的项目中时,为什么会出现此错误?
This is the contents of my <head>
tag, It is how I include jquery.js:
这是我的<head>
标签的内容,这就是我包含 jquery.js 的方式:
<head>
<title>Search Result </title>
<style>
img{ height: 150px; float: left; border: 3;}
div{font-size:10pt; margin-right:150px;
margin-left:150px; }
</style>
<script type="text/javascript" src="jquery-1.6.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){ //Error happens here, $ is not defined.
});
</script>
</head>
回答by Dan Abramov
Did you load jQuery in head
section? Did you load it correctly?
你在head
节中加载了 jQuery吗?你加载正确了吗?
<head>
<script src="scripts/jquery.js"></script>
...
</head>
This code assumes jquery.js
is in scripts
directory. (You can change file name if you like)
此代码假定jquery.js
在scripts
目录中。(您可以根据需要更改文件名)
You can also use jQuery as hosted by Google:
您还可以使用由 Google 托管的jQuery :
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
...
</head>
As per your comment:
根据您的评论:
Apparently, your web server is not configured to return jQuery-1.6.1.js
on requesting /webProject/jquery-1.6.1.js
. There may be numerous reasons for this, such as wrong file name, folder name, routing settings, etc. You need to create another question and describe your 404
in greater details (such as local file name, operation system, webserver name and settings).
显然,您的 Web 服务器未配置为jQuery-1.6.1.js
在请求/webProject/jquery-1.6.1.js
. 这可能有很多原因,例如错误的文件名、文件夹名称、路由设置等。您需要创建另一个问题并404
更详细地描述您的问题(例如本地文件名、操作系统、网络服务器名称和设置)。
Again, you can use jQuery as provided by Google (see above), however you still might want to find out why some local files don't get served on request.
同样,您可以使用 Google 提供的 jQuery(见上文),但是您可能仍然想知道为什么某些本地文件不按请求提供。
回答by abilash kumar
I found we need to use noConflictsometimes:
我发现我们有时需要使用noConflict:
jQuery.noConflict()(function ($) { // this was missing for me
$(document).ready(function() {
other code here....
});
});
From the docs:
从文档:
Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case,
$
is just an alias for jQuery, so all functionality is available without using$
. If you need to use another JavaScript library alongside jQuery, return control of$
back to the other library with a call to$.noConflict()
. Old references of$
are saved during jQuery initialization;noConflict()
simply restores them.
许多 JavaScript 库使用 $ 作为函数或变量名,就像 jQuery 一样。在 jQuery 的情况下,
$
只是 jQuery 的别名,因此所有功能都可用而无需使用$
. 如果您需要与 jQuery 一起使用另一个 JavaScript$
库,请通过调用$.noConflict()
. 旧引用$
在 jQuery 初始化期间保存;noConflict()
简单地恢复它们。
回答by Kokos
You only get this error if jQuery is not correctly loaded, you can check with firebug what url its trying to load so you can see if your relative path is wrong.
如果 jQuery 未正确加载,您只会收到此错误,您可以使用 firebug 检查它尝试加载的 url,这样您就可以查看您的相对路径是否错误。
Also, on a separate note, you can use $(function(){
as a shorthand to $(document).ready(function(){
此外,在单独的说明中,您可以将其$(function(){
用作速记$(document).ready(function(){
回答by live-love
If you have a js file that references the jquery, it could be because the js file is in the body and not in the head section (that was my problem). You should move your js file to the head section AFTER the jquery.js reference.
如果您有一个引用 jquery 的 js 文件,那可能是因为 js 文件在正文中而不是在 head 部分中(这是我的问题)。您应该在 jquery.js 引用之后将您的 js 文件移动到 head 部分。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script>
<script src="myfile.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
回答by Bishnu Paudel
I just had this issue and it was because of me trying to included jQuery via http while my page was loaded as https.
我刚刚遇到了这个问题,这是因为当我的页面作为 https 加载时,我试图通过 http 包含 jQuery。
回答by Chris Tapley
I had this same problem in Chrome where my scripts were like:
我在 Chrome 中遇到了同样的问题,我的脚本如下:
<script src="./scripts/jquery-1.12.3.min.js"></script>
<script src="./scripts/ol-3.15.1/ol.js" />
<script>
...
$(document).ready(function() {
...
});
...
</script>
When I changed to:
当我改为:
<script src="./scripts/jquery-1.12.3.min.js"></script>
<script src="./scripts/ol-3.15.1/ol.js"></script>
$(document).ready was called.
$(document).ready 被调用。
回答by Vivek
see for your js path that may be the causing issue...because You only get this error if jQuery is not correctly loaded.
请查看您的 js 路径,这可能是导致问题的原因...因为如果 jQuery 未正确加载,您只会收到此错误。
回答by Binh Nguyen
I have same issue ... at http://www.xaluan.com.. but after log. I find out that after jQuery run I make a function using one element id which not exists ..
我有同样的问题......在http://www.xaluan.com.. 但在登录后。我发现在 jQuery 运行后,我使用一个不存在的元素 id 创建了一个函数..
Eg:
例如:
$('#aaa').remove() <span class="aaa">sss</spam>
so the id="aaa"
did not exist .. then jQuery stops running.. because errors like that
所以id="aaa"
不存在..然后jQuery停止运行..因为这样的错误
回答by Reign.85
Use:
用:
jQuery(document).ready(function($){
// code where you can use $ thanks to the parameter
});
Or its shorter version:
或者它的较短版本:
jQuery(function($){
// code where you can use $ thanks to the parameter
});
回答by Md.Morshadun Nur
Set events after loading DOM Elements.
在加载 DOM 元素后设置事件。
$(function () {
$(document).on("click","selector",function (e) {
alert("hi");
});
});