JS在页面中的执行顺序

页面从上到下开始加载。执行顺序如下:

1、

 $(function () { 

alert("Loading"); 

$("#Label1").click(function () { 

alert("你点击了Label1"); 

}) 

$("#Label2").click(function () {

 alert("你点击了Label1"); 

}) 

$("#LinkButton1").click(function () { 

alert("你点击了LinkButton1");

 }) 

$("#RadioButton1").click(function () { 

alert("你点击了RadioButton1");

 })

 });

 
 $(function () {
            alert("Loading");
            $("#Label1").click(function () {
                alert("你点击了Label1");
            })
            $("#Label2").click(function () {
                alert("你点击了Label1");
            })
            $("#LinkButton1").click(function () {
                alert("你点击了LinkButton1");
            })
            $("#RadioButton1").click(function () {
                alert("你点击了RadioButton1");
            })
        });


里面的代码在页面加载完之后执行


2、

  alert("NotLoading");
         $(".C").click(function () {
             alert("good");
         })
但是此时不能为 $(".C")加入click事件,因为此时页面还没有生成元素。


3、

页面加载完之后执行body后面的js代码

script type="text/javascript">
     alert("AfterBody");


     $(".C").click(function () {
         alert("good");
     })
    </script>
此时为class为C的元素加入了click事件


4、执行第一步当中

 $(function () {
            alert("Loading");
            $("#Label1").click(function () {
                alert("你点击了Label1");
            })
            $("#Label2").click(function () {
                alert("你点击了Label1");
            })
            $("#LinkButton1").click(function () {
                alert("你点击了LinkButton1");
            })
            $("#RadioButton1").click(function () {
                alert("你点击了RadioButton1");
            })
        });


完整页面如下:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
alert("first");
        $(function () {
            alert("Loading");
            $("#Label1").click(function () {
                alert("你点击了Label1");
            })
            $("#Label2").click(function () {
                alert("你点击了Label1");
            })
            $("#LinkButton1").click(function () {
                alert("你点击了LinkButton1");
            })
            $("#RadioButton1").click(function () {
                alert("你点击了RadioButton1");
            })
        });
      
    </script>
     <script type="text/javascript">
         alert("NotLoading");


         $(".C").click(function () {
             alert("good");
         })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label2"
            runat="server" Text="Label"></asp:Label>
            <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
            <asp:RadioButton
                ID="RadioButton1" runat="server" />


                <p class="C">点我吧!</p>
    </div>
    </form>
  
</body>
 <script type="text/javascript">
     alert("AfterBody");


     $(".C").click(function () {
         alert("good");
     })
    </script>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值