我们可以在asp.net页面中使用$(document).ready()吗?
我尝试使用 $(document).ready() 时遇到错误,如上图所示。 我应该尝试什么来解决这个问题?
编辑 23/05/2011 10:54
我有线索了。 我正在工作的页面继承自母版页
在母版页中此代码不起作用,可能是不同路径的问题
<head id="Head1" runat="server">
<title>Back Office</title>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
<link href="Styles/custom-theme/jquery-ui-1.8.12.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/jquery-1.5.1.min.js"></script>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
此代码似乎不起作用
在母版页的主体上,有一个脚本管理器和jquery, 这使得 jQuery 可以工作,是因为我们在主体中使用了 scriptmanager,所以 document.ready 不起作用?
<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.5.1.min.js" />
</Scripts>
</asp:ScriptManager>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
我已经发现问题了。
问题是 jquery 代码位于母版页上的脚本管理器中,而脚本管理器位于 body 标记内。
document.ready 位于继承母版页的页面标题标记上,因此在执行到正文的代码之前,尚未包含 jquery,这就是错误。
解决这个问题最简单的方法是,我必须将 JavaScript 代码移动到 body 标记的底部。 正确的解决方法(我仍然找不到)是将 javascript 包含代码放在标头中。但我还没有找到有效的方法。我的母版页和继承页位于不同的路径上。我发现了很多技术,但没有一个有效。
I have an error trying to use $(document).ready() as in the above image.
What should i try to solve this problem ?
Edit 23/05/2011 10:54
I have got a clue.
The page i am working inherit from masterpage
In master page this code isn't work, maybe problem of different path
<head id="Head1" runat="server">
<title>Back Office</title>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
<link href="Styles/custom-theme/jquery-ui-1.8.12.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="scripts/jquery-1.5.1.min.js"></script>
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
This code don't seems to work
On the body of master page, there is a script manager and jquery is there,
This make the jQuery works, is because we use scriptmanager in the body so that the document.ready is not work ?
<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
<Scripts>
<asp:ScriptReference Path="~/Scripts/jquery-1.5.1.min.js" />
</Scripts>
</asp:ScriptManager>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
I have found the problems.
The problem is the jquery code is on the masterpage, in the script manager, and script manager is in inside the body tag.
the document.ready is on the header tag on page that inherit on master page, so before the code to excuting to the body, the jquery is not included yet and that wat it is error.
The easiest way to solve this is, i have to move javascript code in the bottom of body tag.
The proper way to solve, which i still can't find is to put the javascript include code in the header. but i don't find a way that work yet. My master page and inherit page is on the differnt path. I have find a lot of technique such as but none of them work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除除 jquery-1.5.1.min.js 之外的所有其他 javascript。尝试仅使用该包含的一个。如果有效,请一次将其他包含项添加回其中,直到出现错误为止。如果它不能仅使用包含的 jQuery 脚本,则很可能它没有指向正确的路径。
简短的回答是,是的,您可以在 ASP.NET 页面中使用 jQuery 和 document.ready。
Take out all of the other javascript includes except for jquery-1.5.1.min.js. Try it with just that one include. If that works, add the other includes back in one at a time until you get the error. If it doesn't work with just the jQuery script included, chances are it's not pointing at the correct path.
Short answer though, yes, you can use jQuery and document.ready in an ASP.NET page.
当然可以。然而,似乎 jQuery 对象在脚本执行时不可用。验证 jquery 库文件确实已通过脚本指令加载。或者这背后可能还有其他一些细微差别。
Surely you can. Seems, however that jQuery object is not available at the moment of script execution. Verify that jquery library file is indeed loaded via script directive. Or maybe there are some other nuances behind this.