简体   繁体   English

我们可以在asp.net页面中使用$(document).ready()吗?

[英]Can we use $(document).ready() in asp.net page?

在此输入图像描述

I have an error trying to use $(document).ready() as in the above image. 我在尝试使用$(document).ready()时出错,如上图所示。 What should i try to solve this problem ? 我应该尝试解决这个问题?


Edit 23/05/2011 10:54 编辑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 ? 在母版页的主体上,有一个脚本管理器和jquery在那里,这使得jQuery工作,是因为我们在body中使用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>

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. 问题是jquery代码在masterpage页面上,在脚本管理器中,而脚本管理器在body标签内。

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. document.ready位于页面上继承在母版页上的标题标记上,因此在将代码排除到正文之前,还没有包含jquery,并且它是错误的。

The easiest way to solve this is, i have to move javascript code in the bottom of body tag. 解决这个问题最简单的方法是,我必须在body标签的底部移动javascript代码。 The proper way to solve, which i still can't find is to put the javascript include code in the header. 正确的解决方法,我仍然无法找到将javascript包含在头文件中的代码。 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. 我找到了很多技术,但没有一个能奏效。

Take out all of the other javascript includes except for jquery-1.5.1.min.js. 取出除jquery-1.5.1.min.js之外的所有其他javascript包含。 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. 如果它不适用于仅包含的jQuery脚本,则可能不是指向正确的路径。

Short answer though, yes, you can use jQuery and document.ready in an ASP.NET page. 简而言之,是的,您可以在ASP.NET页面中使用jQuery和document.ready。

Surely you can. 你当然可以。 Seems, however that jQuery object is not available at the moment of script execution. 似乎jQuery对象在脚本执行时不可用。 Verify that jquery library file is indeed loaded via script directive. 验证是否确实通过脚本指令加载了jquery库文件。 Or maybe there are some other nuances behind this. 或许这背后还有其他一些细微差别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.net(Document).ready()无法运行 - ASP.net (Document).ready() failing to run ASP.NET - jQuery (document).ready() 函数不会被用户控制触发 - ASP.NET - jQuery (document).ready() function is not fired for user control 不能在ASP.net文档的onload属性中使用“ this” - Can't use 'this' in onload property in an ASP.net document 我们可以在ASP.NET页面上显示来自Javascript警报的超链接吗? - Can we display Hyperlink from Javascript alert on ASP.NET Page? jQuery document.ready + Asp.Net ContentPlaceholder导致Visual Studio智能问题 - jQuery document.ready + Asp.Net ContentPlaceholder cause Visual Studio intellisence problems 获取在document.ready中设置的隐藏字段的值,该值位于asp.net代码后面 - Grab hidden field's value that is set in document.ready in asp.net code behind ASP.NET MVC,IE 11,$(document).ready和向后导航缓存 - ASP.NET MVC, IE 11, $(document).ready and back navigation caching 在asp.net中获取pdf文档的当前页码 - get current page number of pdf document in asp.net 在ASP.net中使用的文本编辑器是否有等效的JSP - Is there any JSP equivalent for text editor as we use in ASP.net 我们可以将整个 model 传递给 javascript asp.net mvc - can we pass entire model to javascript asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM