首页 技术 正文
技术 2022年11月12日
0 收藏 413 点赞 3,162 浏览 12277 个字

本节内容:

  • javascript作用域
  • DOM收尾

JavaScript作用域

JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕…

一、JavaScript中无块级作用域

在Java或C#中存在块级作用域,即:大括号也是一个作用域。

 public static void main () {     if(1==1){         String name = "seven";     }     System.out.println(name); } // 报错

java

 public static void Main() {     if(1==1){         string name = "seven";     }     Console.WriteLine(name); } // 报错

C#

在JavaScript语言中无块级作用域

 function Main(){     if(1==1){         var name = 'seven';     }     console.log(name); } // 输出: seven

补充:标题之所以添加双引号是因为JavaScript6中新引入了 let 关键字,用于指定变量属于块级作用域。

二、JavaScript采用函数作用域

在JavaScript中每个函数作为一个作用域,在外部无法访问内部作用域中的变量。

 function Main(){     var innerValue = 'seven'; } Main(); console.log(innerValue); // 报错:Uncaught ReferenceError: innerValue is not defined

三、JavaScript的作用域链

由于JavaScript中的每个函数作为一个作用域,如果出现函数嵌套函数,则就会出现作用域链。

 xo = 'alex'; function Func(){     var xo = "seven";     function inner(){         var xo = 'alvin';         console.log(xo);     }     inner(); } Func();

如上述代码则出现三个作用域组成的作用域链,如果出现作用域链后,那么寻找变量时候就会出现顺序,对于上述实例:

当执行console.log(xo)时,其寻找顺序为根据作用域链从内到外的优先级寻找,如果内层没有就逐步向上找,直到没找到抛出异常。

四、JavaScript的作用域链执行前已创建

JavaScript的作用域在被执行之前已经创建,日后再去执行时只需要按照作用域链去寻找即可。

示例一:

 xo = 'alex'; function Func(){     var xo = "seven";     function inner(){         console.log(xo);     }     return inner; } var ret = Func(); ret(); // 输出结果: seven

上述代码,在函数被调用之前作用域链已经存在:

    • 全局作用域 -> Func函数作用域 -> inner函数作用域

当执行【ret();】时,由于其代指的是inner函数,此函数的作用域链在执行之前已经被定义为:全局作用域 -> Func函数作用域 -> inner函数作用域,所以,在执行【ret();】时,会根据已经存在的作用域链去寻找变量。

示例二:

 xo = 'alex'; function Func(){     var xo = "eirc";     function inner(){         console.log(xo);     }     xo = 'seven';     return inner; } var ret = Func(); ret(); // 输出结果: seven

上述代码和示例一的目的相同,也是强调在函数被调用之前作用域链已经存在:

    • 全局作用域 -> Func函数作用域 -> inner函数作用域

不同的时,在执行【var ret = Func();】时,Func作用域中的xo变量的值已经由 “eric” 被重置为 “seven”,所以之后再执行【ret();】时,就只能找到“seven”。

示例三:

 xo = 'alex';<br> function Bar(){     console.log(xo); } function Func(){     var xo = "seven";     return Bar; } var ret = Func(); ret(); // 输出结果: alex

上述代码,在函数被执行之前已经创建了两条作用域链:

    • 全局作用域 -> Bar函数作用域
    • 全局作用域 -> Func函数作用域

当执行【ret();】时,ret代指的Bar函数,而Bar函数的作用域链已经存在:全局作用域 -> Bar函数作用域,所以,执行时会根据已经存在的作用域链去寻找。

五、声明提前

在JavaScript中如果不创建变量,直接去使用,则报错:

console.log(xxoo);// 报错:Uncaught ReferenceError: xxoo is not defined

JavaScript中如果创建值而不赋值,则该值为 undefined,如:

 var xxoo; console.log(xxoo); // 输出:undefined

在函数内如果这么写:

 function Foo(){     console.log(xo);     var xo = 'seven'; } Foo(); // 输出:undefined

上述代码,不报错而是输出 undefined,其原因是:JavaScript的函数在被执行之前,会将其中的变量全部声明,而不赋值。所以,相当于上述实例中,函数在“预编译”时,已经执行了var xo;所以上述代码中输出的是undefined。

DOM

二、操作(接第十五篇)

5、样式操作

 var obj = document.getElementById('i1') obj.style.fontSize = "32px"; obj.style.backgroundColor = "red";
  <input onfocus="Focus(this);" onblur="Blur(this);" id="search" value="请输入关键字" style="color: gray;" />     <script>         function Focus(ths){             ths.style.color = "black";             if(ths.value == '请输入关键字' || ths.value.trim() == ""){                 ths.value = "";             }         }         function Blur(ths){             if(ths.value.trim() == ""){                 ths.value = '请输入关键字';                 ths.style.color = 'gray';             }else{                 ths.style.color = "black";             }         }     </script>

demo

6、位置操作

 总文档高度 document.documentElement.offsetHeight 当前文档占屏幕高度 document.documentElement.clientHeight 自身高度 tag.offsetHeight 距离上级定位高度 tag.offsetTop 父定位标签 tag.offsetParent 滚动高度 tag.scrollTop /*     clientHeight -> 可见区域:height + padding     clientTop    -> border高度     offsetHeight -> 可见区域:height + padding + border     offsetTop    -> 上级定位标签的高度     scrollHeight -> 全文高:height + padding     scrollTop    -> 滚动高度     特别的:         document.documentElement代指文档根节点 */
 <!DOCTYPE html> <html> <head lang="en">     <meta charset="UTF-8">     <title></title> </head> <body style="margin: 0;">     <div style="height: 900px;">     </div>     <div style="padding: 10px;">         <div id="i1" style="height:190px;padding: 2px;border: 1px solid red;margin: 8px;">                 <p>asdf</p>                 <p>asdf</p>                 <p>asdf</p>                 <p>asdf</p>                 <p>asdf</p>         </div>     </div>     <script>         var i1 = document.getElementById('i1');         console.log(i1.clientHeight); // 可见区域:height + padding         console.log(i1.clientTop);    // border高度         console.log('=====');         console.log(i1.offsetHeight); // 可见区域:height + padding + border         console.log(i1.offsetTop);    // 上级定位标签的高度         console.log('=====');         console.log(i1.scrollHeight);   //全文高:height + padding         console.log(i1.scrollTop);      // 滚动高度         console.log('=====');     </script> </body> </html> test

test

 <!DOCTYPE html> <html> <head lang="en">     <meta charset="UTF-8">     <title></title> </head> <style>     body{         margin: 0px;     }     img {         border: 0;     }     ul{         padding: 0;         margin: 0;         list-style: none;     }     .clearfix:after {         content: ".";         display: block;         height: 0;         clear: both;         visibility: hidden;     }     .wrap{         width: 980px;         margin: 0 auto;     }     .pg-header{         background-color: #303a40;         -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);         -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);         box-shadow: 0 2px 5px rgba(0,0,0,.2);     }     .pg-header .logo{         float: left;         padding:5px 10px 5px 0px;     }     .pg-header .logo img{         vertical-align: middle;         width: 110px;         height: 40px;     }     .pg-header .nav{         line-height: 50px;     }     .pg-header .nav ul li{         float: left;     }     .pg-header .nav ul li a{         display: block;         color: #ccc;         padding: 0 20px;         text-decoration: none;         font-size: 14px;     }     .pg-header .nav ul li a:hover{         color: #fff;         background-color: #425a66;     }     .pg-body{     }     .pg-body .catalog{         position: absolute;         top:60px;         width: 200px;         background-color: #fafafa;         bottom: 0px;     }     .pg-body .catalog.fixed{         position: fixed;         top:10px;     }     .pg-body .catalog .catalog-item.active{         color: #fff;         background-color: #425a66;     }     .pg-body .content{         position: absolute;         top:60px;         width: 700px;         margin-left: 210px;         background-color: #fafafa;         overflow: auto;     }     .pg-body .content .section{         height: 500px;     } </style> <body onscroll="ScrollEvent();"> <div class="pg-header">     <div class="wrap clearfix">         <div class="logo">             <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >                 <img src="http://core.pc.lietou-static.com/revs/images/common/logo_7012c4a4.pn">             </a>         </div>         <div class="nav">             <ul>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >首页</a>                 </li>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >功能一</a>                 </li>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >功能二</a>                 </li>             </ul>         </div>     </div> </div> <div class="pg-body">     <div class="wrap">         <div class="catalog">             <div class="catalog-item" auto-to="function1"><a>第1张</a></div>             <div class="catalog-item" auto-to="function2"><a>第2张</a></div>             <div class="catalog-item" auto-to="function3"><a>第3张</a></div>         </div>         <div class="content">             <div menu="function1" class="section">                 <h1>第一章</h1>             </div>             <div menu="function2" class="section">                 <h1>第二章</h1>             </div>             <div menu="function3" class="section">                 <h1>第三章</h1>             </div>         </div>     </div> </div>     <script>         function ScrollEvent(){             var bodyScrollTop = document.body.scrollTop;             if(bodyScrollTop>50){                 document.getElementsByClassName('catalog')[0].classList.add('fixed');             }else{                 document.getElementsByClassName('catalog')[0].classList.remove('fixed');             }         }     </script> </body> </html>

demo-滚动固定

 <!DOCTYPE html> <html> <head lang="en">     <meta charset="UTF-8">     <title></title> </head> <style>     body{         margin: 0px;     }     img {         border: 0;     }     ul{         padding: 0;         margin: 0;         list-style: none;     }     h1{         padding: 0;         margin: 0;     }     .clearfix:after {         content: ".";         display: block;         height: 0;         clear: both;         visibility: hidden;     }     .wrap{         width: 980px;         margin: 0 auto;     }     .pg-header{         background-color: #303a40;         -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);         -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);         box-shadow: 0 2px 5px rgba(0,0,0,.2);     }     .pg-header .logo{         float: left;         padding:5px 10px 5px 0px;     }     .pg-header .logo img{         vertical-align: middle;         width: 110px;         height: 40px;     }     .pg-header .nav{         line-height: 50px;     }     .pg-header .nav ul li{         float: left;     }     .pg-header .nav ul li a{         display: block;         color: #ccc;         padding: 0 20px;         text-decoration: none;         font-size: 14px;     }     .pg-header .nav ul li a:hover{         color: #fff;         background-color: #425a66;     }     .pg-body{     }     .pg-body .catalog{         position: absolute;         top:60px;         width: 200px;         background-color: #fafafa;         bottom: 0px;     }     .pg-body .catalog.fixed{         position: fixed;         top:10px;     }     .pg-body .catalog .catalog-item.active{         color: #fff;         background-color: #425a66;     }     .pg-body .content{         position: absolute;         top:60px;         width: 700px;         margin-left: 210px;         background-color: #fafafa;         overflow: auto;     }     .pg-body .content .section{         height: 500px;         border: 1px solid red;     } </style> <body onscroll="ScrollEvent();"> <div class="pg-header">     <div class="wrap clearfix">         <div class="logo">             <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >                 <img src="http://core.pc.lietou-static.com/revs/images/common/logo_7012c4a4.pn">             </a>         </div>         <div class="nav">             <ul>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >首页</a>                 </li>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >功能一</a>                 </li>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >功能二</a>                 </li>             </ul>         </div>     </div> </div> <div class="pg-body">     <div class="wrap">         <div class="catalog" id="catalog">             <div class="catalog-item" auto-to="function1"><a>第1张</a></div>             <div class="catalog-item" auto-to="function2"><a>第2张</a></div>             <div class="catalog-item" auto-to="function3"><a>第3张</a></div>         </div>         <div class="content" id="content">             <div menu="function1" class="section">                 <h1>第一章</h1>             </div>             <div menu="function2" class="section">                 <h1>第二章</h1>             </div>             <div menu="function3" class="section">                 <h1>第三章</h1>             </div>         </div>     </div> </div>     <script>         function ScrollEvent(){             var bodyScrollTop = document.body.scrollTop;             if(bodyScrollTop>50){                 document.getElementsByClassName('catalog')[0].classList.add('fixed');             }else{                 document.getElementsByClassName('catalog')[0].classList.remove('fixed');             }             var content = document.getElementById('content');             var sections = content.children;             for(var i=0;i<sections.length;i++){                 var current_section = sections[i];                 // 当前标签距离顶部绝对高度                 var scOffTop = current_section.offsetTop + 60;                 // 当前标签距离顶部,相对高度                 var offTop = scOffTop - bodyScrollTop;                 // 当前标签高度                 var height = current_section.scrollHeight;                 if(offTop<0 && -offTop < height){                     // 当前标签添加active                     // 其他移除 active                     var menus = document.getElementById('catalog').children;                     var current_menu = menus[i];                     current_menu.classList.add('active');                     for(var j=0;j<menus.length;j++){                         if(menus[j] == current_menu){                         }else{                             menus[j].classList.remove('active');                         }                     }                     break;                 }             }         }     </script> </body> </html>

demo-滚动菜单

 <!DOCTYPE html> <html> <head lang="en">     <meta charset="UTF-8">     <title></title> </head> <style>     body{         margin: 0px;     }     img {         border: 0;     }     ul{         padding: 0;         margin: 0;         list-style: none;     }     h1{         padding: 0;         margin: 0;     }     .clearfix:after {         content: ".";         display: block;         height: 0;         clear: both;         visibility: hidden;     }     .wrap{         width: 980px;         margin: 0 auto;     }     .pg-header{         background-color: #303a40;         -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);         -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);         box-shadow: 0 2px 5px rgba(0,0,0,.2);     }     .pg-header .logo{         float: left;         padding:5px 10px 5px 0px;     }     .pg-header .logo img{         vertical-align: middle;         width: 110px;         height: 40px;     }     .pg-header .nav{         line-height: 50px;     }     .pg-header .nav ul li{         float: left;     }     .pg-header .nav ul li a{         display: block;         color: #ccc;         padding: 0 20px;         text-decoration: none;         font-size: 14px;     }     .pg-header .nav ul li a:hover{         color: #fff;         background-color: #425a66;     }     .pg-body{     }     .pg-body .catalog{         position: absolute;         top:60px;         width: 200px;         background-color: #fafafa;         bottom: 0px;     }     .pg-body .catalog.fixed{         position: fixed;         top:10px;     }     .pg-body .catalog .catalog-item.active{         color: #fff;         background-color: #425a66;     }     .pg-body .content{         position: absolute;         top:60px;         width: 700px;         margin-left: 210px;         background-color: #fafafa;         overflow: auto;     }     .pg-body .content .section{         height: 500px;         border: 1px solid red;     } </style> <body onscroll="ScrollEvent();"> <div class="pg-header">     <div class="wrap clearfix">         <div class="logo">             <a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >                 <img src="http://core.pc.lietou-static.com/revs/images/common/logo_7012c4a4.pn">             </a>         </div>         <div class="nav">             <ul>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >首页</a>                 </li>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >功能一</a>                 </li>                 <li>                     <a  href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >功能二</a>                 </li>             </ul>         </div>     </div> </div> <div class="pg-body">     <div class="wrap">         <div class="catalog" id="catalog">             <div class="catalog-item" auto-to="function1"><a>第1张</a></div>             <div class="catalog-item" auto-to="function2"><a>第2张</a></div>             <div class="catalog-item" auto-to="function3"><a>第3张</a></div>         </div>         <div class="content" id="content">             <div menu="function1" class="section">                 <h1>第一章</h1>             </div>             <div menu="function2" class="section">                 <h1>第二章</h1>             </div>             <div menu="function3" class="section" style="height: 200px;">                 <h1>第三章</h1>             </div>         </div>     </div> </div>     <script>         function ScrollEvent(){             var bodyScrollTop = document.body.scrollTop;             if(bodyScrollTop>50){                 document.getElementsByClassName('catalog')[0].classList.add('fixed');             }else{                 document.getElementsByClassName('catalog')[0].classList.remove('fixed');             }             var content = document.getElementById('content');             var sections = content.children;             for(var i=0;i<sections.length;i++){                 var current_section = sections[i];                 // 当前标签距离顶部绝对高度                 var scOffTop = current_section.offsetTop + 60;                 // 当前标签距离顶部,相对高度                 var offTop = scOffTop - bodyScrollTop;                 // 当前标签高度                 var height = current_section.scrollHeight;                 if(offTop<0 && -offTop < height){                     // 当前标签添加active                     // 其他移除 active                     // 如果已经到底部,现实第三个菜单                     // 文档高度 = 滚动高度 + 视口高度                     var a = document.getElementsByClassName('content')[0].offsetHeight + 60;                     var b = bodyScrollTop + document.documentElement.clientHeight;                     console.log(a+60,b);                     if(a == b){                         var menus = document.getElementById('catalog').children;                         var current_menu = document.getElementById('catalog').lastElementChild;                         current_menu.classList.add('active');                         for(var j=0;j<menus.length;j++){                             if(menus[j] == current_menu){                             }else{                                 menus[j].classList.remove('active');                             }                         }                     }else{                         var menus = document.getElementById('catalog').children;                         var current_menu = menus[i];                         current_menu.classList.add('active');                         for(var j=0;j<menus.length;j++){                             if(menus[j] == current_menu){                             }else{                                 menus[j].classList.remove('active');                             }                         }                     }                     break;                 }             }         }     </script> </body> </html>

demo-滚动高度

7、提交表单

document.geElementById('form').submit()

8、其他操作

 console.log                 输出框 alert                       弹出框 confirm                     确认框 // URL和刷新 location.href               获取URL location.href = "url"       重定向 location.reload()           重新加载 // 定时器 setInterval                 多次定时器 clearInterval               清除多次定时器 setTimeout                  单次定时器 clearTimeout                清除单次定时器

三、事件

对于事件需要注意的要点:

    • this
    • event
    • 事件链以及跳出

this标签当前正在操作的标签,event封装了当前事件的内容。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849