我将 JQuery 添加到我的 asp.net 网页。我无法完全理解运行顺序。所以我写了下面的代码来测试它。无论如何,Page_Load都会比$(document).ready()先运行,对吗?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
alert('Hell');
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = "World";
}
}
}
最佳答案
Page_Load
是服务器端事件。 $(document).ready()
是客户端。
呈现页面的服务器事件将始终首先运行。服务器事件可能会运行额外的时间来响应异步回发,但初始渲染始终会从服务器到客户端发生,而不是相反。
两个事件之间没有直接关系,即一个事件的存在并不真正需要另一个事件的存在。您可以编写一个 ASP.NET 页面,将内容呈现到二进制流(因此没有文档事件),或者您可以编写一个没有服务器代码(并且没有 OnLoad 方法)的纯 HTML 页面。
关于asp.net - Page_Load(object sender, EventArgs e) 和 $(document).ready(function () 之间哪个先运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677626/