jQuery $(document).ready 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6085964/
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 not Working
提问by paul simmons
I am trying to run jQuery and WebMethods with ASP.NET; I have added a ScriptManager to the master page, and a content on the content page
我正在尝试使用 ASP.NET 运行 jQuery 和 WebMethods;我已将 ScriptManager 添加到母版页,并在内容页上添加了内容
<asp:Content ID="ch" ContentPlaceHolderID="cHead" runat="server">
<script language="javascript" type="text/javascript">
$(document).ready(function () {
alert("hi");
});
</script>
</asp:Content>
However this never fires, what am I missing?
但是这永远不会触发,我错过了什么?
回答by kobe
Verify the following steps.
验证以下步骤。
- Did you include jquery
- Check for errors in firebug
- 你有没有包括jquery
- 检查 firebug 中的错误
Those things should fix the problem
这些东西应该可以解决问题
回答by Dave Cousineau
One possibility when ready
stops working is that you have javascript code somewhere that is throwing an exception in a $(document).ready(...)
or $(...)
call, which is stopping processing of the rest of the ready
blocks. Identify a single page on which this is occurring and examine it for possible errors in other places.
ready
停止工作的一种可能性是您在某处有 javascript 代码在$(document).ready(...)
or$(...)
调用中抛出异常,这会停止处理其余ready
块。确定发生这种情况的单个页面,并检查其他地方是否存在可能的错误。
回答by Harshal Lonare
Instead of using this:
而不是使用这个:
$(document).ready(function() { /* your code */ });
Use this:
用这个:
jQuery(function($) { /* your code */ })(jQuery);
It is more concise and does the same thing, it also doesn't depend on the $
variable to be the jQuery object.
它更简洁并且做同样的事情,它也不依赖于$
变量成为 jQuery 对象。
回答by Richard
This will happen if the host page is HTTPS and the included javascript source path is HTTP. The two protocols must be the same, HTTPS. The tell tail sign would be to check under Firebug and notice that the JS is "denied access".
如果主机页面是 HTTPS 并且包含的 javascript 源路径是 HTTP,则会发生这种情况。这两个协议必须相同,HTTPS。告诉尾部标志将在 Firebug 下检查并注意 JS 是“拒绝访问”。
回答by Mohd Abdul Mujib
I had copy pasted my inline js
from some other .php
project, inside that block of code there was some php
code outputting some value, now since the variable wasn't defined in my new file, it was producing the typical php
undefined warning/error
, and because of that the js
code was being messed up, and wasn't responding to any event, even alert("xyz");
would fail silently!! Although the erronous line was way near the end of the file, still the js
would just die that too,
我已经js
从其他.php
项目中复制粘贴了我的内联代码,在该代码块中,有一些php
代码输出了一些值,现在由于我的新文件中没有定义该变量,它产生了典型php
undefined warning/error
的 ,因此js
代码是被搞砸了,对任何事件都没有反应,甚至alert("xyz");
会默默失败!!虽然错误的行已经接近文件的末尾,但仍然js
会死掉,
without any errors!!! >:(
没有任何错误!!!>:(
Now one thing confusing is that debugger console/output gave no hint/error/warning whatsoever, the js
was dying silently.
现在令人困惑的一件事是调试器控制台/输出没有给出任何提示/错误/警告,它js
正在默默地死去。
So try checking if you have php
inline coded with the js
, and see if it is outputting any error. Once removed/sorted your js
should work fine.
因此,请尝试检查您是否使用 进行了php
内联编码js
,并查看它是否输出任何错误。删除/排序后,您js
应该可以正常工作。
回答by Rom Mil
function pageLoad() { console.log('pageLoad'); $(document).ready(function () { alert("hi"); }); };
function pageLoad() { console.log('pageLoad'); $(document).ready(function () { alert("hi"); }); };
its the ScriptManager ajax making the problem use pageLoad() instead
它的 ScriptManager ajax 使问题使用 pageLoad() 代替
回答by Stormborn
I had a similar problem, but I got it solved this way:
我有一个类似的问题,但我是这样解决的:
// wait until DOM is loaded
$(document).ready(function(){
console.log("DOM is ready");
// wait until window is loaded (images, links etc...)
window.onload = function() {
console.log("window is loaded");
var your_html_element = document.getElementById("your_html_element_ID"),
};
});
回答by MohammadSoori
- Check for your script has to be write or loaded after jQuery link.
- 检查您的脚本是否必须在 jQuery 链接之后写入或加载。
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
//after link >> write codes...
<script>
$(document).ready(function(){
//your code
})(jQuery);
</script>
回答by agodinhost
One possibility, take a look:
一种可能,看看:
<script language="javascript" type="text/javascript" src="./jquery-3.1.1.slim.min.js" />
vs
对比
<script language="javascript" type="text/javascript" src="./jquery-3.1.1.slim.min.js"></script>
The first method is actually wrong, however the browser does not complain at all. You need to be sure that you are indeed closing the javascript tag in the proper way.
第一种方法实际上是错误的,但是浏览器根本没有抱怨。您需要确保您确实以正确的方式关闭了 javascript 标记。