javascript jquery "$(document).ready(function () {" 在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5430644/
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 "$(document).ready(function () {" isn't working in IE
提问by WillMcKill
Im using the MVC 2 framwork and have added some javascript for expanding divs. It works fine in firefox, chrome, opera and safari but not in internet explorer. I get an 'Object Expected' Error. Here is my code
我使用 MVC 2 框架并添加了一些用于扩展 div 的 javascript。它在 firefox、chrome、opera 和 safari 中运行良好,但在 Internet Explorer 中不起作用。我收到“预期对象”错误。这是我的代码
the jquery import is in the site.master file
jquery 导入在 site.master 文件中
<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" charset="utf-8"></script>
The Javascript, in the mvc view: the test alert comes up in IE but test2 doesn't.
Javascript,在 mvc 视图中:测试警报出现在 IE 中,但 test2 没有。
<script type="text/javascript">
alert("test");
$(document).ready(function () {
alert("test2");
$(".expandingcontent").hide();
$(".divexpand").click(function () {
var divID = "#" + $(this).attr("id").substring(6);
if ($(divID).is(":hidden")) {
$(divID).slideDown("slow");
} else {
$(divID).hide();
}
});
});
</script>
Ive tried placing the javavscript at the beging of the page, at the end nothing seems to work. Ive also tried using a timeout but no success there either. I'm using IE 8, any help is very much appreciated Thanks!
我试过将 javascript 放在页面的开头,最后似乎没有任何效果。我也试过使用超时,但也没有成功。我正在使用 IE 8,非常感谢任何帮助谢谢!
采纳答案by Yngve B-Nilsen
It might be caused by it not being able to load the jquery.js? Try downloading it and putting it as a local resource.
可能是因为无法加载jquery.js?尝试下载它并将其作为本地资源。
回答by naveen
Your reference to Google CDN resource is wrong, especially if your site is using SSL.
Change your reference like this and it should work.
您对 Google CDN 资源的引用是错误的,尤其是当您的站点使用 SSL 时。
像这样更改您的参考,它应该可以工作。
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
Its because,
这是因为,
It's not exactly light reading, but section 4.2 of RFC 3986provides for fully qualified URLs that omit protocol (the HTTP or HTTPS) altogether. When a URL's protocol is omitted, the browser uses the underlying document's protocol instead.
这不是完全轻松的阅读,但 RFC 3986 的第 4.2 节提供了完全限定的 URL,它们完全省略了协议(HTTP 或 HTTPS)。当省略 URL 的协议时,浏览器将使用底层文档的协议。
Dave Ward explains it more: Cripple the Google CDN's caching with a single character
戴夫·沃德 (Dave Ward) 对此进行了更多解释:用单个字符削弱 Google CDN 的缓存
P.S: Its better to use Google CDN, together with a local fallback resource in case CDN failsto load
PS:最好使用谷歌CDN,加上本地回退资源,以防CDN加载失败
回答by tobias86
Make sure to include the jquery script file before you execute the $(document).ready function.
确保在执行 $(document).ready 函数之前包含 jquery 脚本文件。
回答by Dan Caddigan
In my case, I had an extraneous console.log in the JS and it quietly broke in IE. It was confusing because when I enabled the IE developer tools it magically started working. Anyway, check that, too.
就我而言,我在 JS 中有一个无关的 console.log,它在 IE 中悄悄地崩溃了。这很令人困惑,因为当我启用 IE 开发人员工具时,它神奇地开始工作。无论如何,也要检查一下。