HTML+JS 代码块加载顺序

Index.html

<!DOCTYPEhtml>

<html>

<head>

<metacharset="UTF-8">

<title></title>

<scriptsrc="jqueryUi/js/jquery-1.9.1.min.js"></script>

<scriptsrc="testjs.js"></script>

<script>

$(function(){

alert("当前页面中的$(function(){})1");

});

alert("head中的直接alert");

functiontext1(){

alert("页面中的函数调用text1()");

}

text1();

vartestfun=function(){

vartextvalue = $("#textvalue").val();

alert(textvalue);//undefined

alert("页面中的函数调用testfun()");

}

testfun();

$(function(){

alert("当前页面中的$(function(){}2);");

vartextvalue = $("#textvalue").val();

alert(textvalue);//undefined

});

functiontextonload(){

alert("页面加载完以后最后加载的数据");

}

</script>

<scriptsrc="testjs2.js"></script>

</head>

<bodyοnlοad="textonload()">

<inputtype="text" id="textvalue" value="11111"/>

<script>

alert("body中的弹出层");

</script>

</body>

</html>

 

Testjs.js

alert("text1");

function text1(){

alert("textjs");

}

alert("text1");

$(function(){

alert("text1.js中的$(function(){})");

});

 

Testjs2.js

alert("text2");

function text2(){

alert("textjs");

}

alert("text2");

$(function(){

alert("text2.js中的$(function(){})");

});

 

//执行顺序

  1. Text1
  2. Text1
  3. head中的直接alert
  4. 页面中的函数调用text1()
  5. textvalue取值=undefined
  6. 页面中的函数调用testfun()
  7. Text2
  8. Text2
  9. body中的弹出层
  10. text1.js中的$(function(){})
  11. 当前页面中的$(function(){})1
  12. 当前页面中的$(function(){}2);
  13. textvalue取值=11111
  14. text1.js中的$(function(){})
  15. 页面加载完以后最后加载的数据

总结:

  1. html页面中的来说是按照顺序向下执行的;
  2. 外链js数据是也是按照页面的引入顺序执行的;(其实可以看做引入的js就在当前引入的位置);
  3. $(function(){})这种代码块的数据会在页面等加载完以后最后才执行这些代码块;
  4. textvalue取值=undefined是因为页面的标签还未渲染;所以我们获取不到值;
  5. 通常函数的调用我们都是在页面全部渲染完才开始操作的;所以不会出现(undefined
  6. 如果需要页面初始化的时候获取后台数据做渲染;那么我们应该在$(function(){})执行;或者使用onload事件函数在页面加载完以后对数据做出页面渲染。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值