1、全局变量

  • 在函数外面声明的变量,是一个全局变量

全局变量是window的一个属性

  1. <script>
  2. /* 在函数外面声明的变量,是一个全局变量
  3. 全局变量是window的一个属性
  4. */
  5. var a = 10;
  6. console.log(window.a)
  7. </script>

image.png

2、局部变量

  • 作用域:就是一个变量能够生效的范围

    1. <script>
    2. /* 作用域:就是一个变量能够生效的范围 */
    3. var a = 10;
    4. function go(){
    5. var b =20;
    6. console.log(b);
    7. }
    8. console.log(a);
    9. </script>

    image.png

    3、声明提前

  • JS在执行代码的时候,会将所有使用var声明的代码,放在作用域的顶层集中创建,赋值留在原地

  • function函数声明也会提前

    1. <script>
    2. /*
    3. JS在执行代码的时候,会将所有使用var声明的代码,放在作用域的顶层集中创建,赋值留在原地
    4. */
    5. //var a,b;
    6. console.log(a);
    7. var a=10;
    8. var b=20;
    9. console.log(b);
    10. </script>

    image.png

    4、window的方法

  • window方法的特点:window.是可以省略的

1、window是访问窗口的一个接口
2、是一个全局对象

4-1.window提示框

  1. <script>
  2. /* window方法的特点:window.是可以省略的 */
  3. alert("hello world")
  4. /*
  5. 1、window是访问窗口的一个接口
  6. 2、是一个全局对象
  7. */
  8. </script>

image.png

4-2.window定义变量

  1. <script>
  2. window.b = 20; //var b = 20;
  3. var a = 30;
  4. console.log(a+b);
  5. </script>

image.png

4-3.window输入框

  • 有值则true
  • 无值则false

    1. <script>
    2. var b = prompt("请输入");
    3. console.log(b);
    4. if(b){
    5. alert("输入成功")
    6. }else{
    7. alert("不能没有输入")
    8. }
    9. </script>

    image.png

    4-4.window选择框

  • 确定则true

  • 取消则false

    1. <script>
    2. var a = window.confirm("去不去");
    3. console.log(a);
    4. </script>

    image.png

    4-5.点击确认删除事件

  • 点击删除后,confirm选择值返回为true确认删除

    1. <body>
    2. <ul>
    3. <li>html<button>删除</button></li>
    4. <li>css<button>删除</button></li>
    5. <li>javascript<button>删除</button></li>
    6. </ul>
    7. <script>
    8. var btns = document.getElementsByTagName("button");
    9. /* 1、给每个btn绑定点击事件 */
    10. for(var i=0;i<btns.length;i++){
    11. btns[i].onclick = function(){
    12. /* 2、点击对应的元素,将其父元素删除 */
    13. var isShow = confirm("是否删除");
    14. console.log(isShow);
    15. if(isShow){
    16. this.parentNode.style.display = "none";
    17. }
    18. }
    19. }
    20. </script>
    21. </body>

    image.png

    4-6.history历史记录

  • history.back() 回退到前一个界面

    1. <body>
    2. <a href="09history.html">前往09</a>
    3. </body>
    1. <body>
    2. <button id="btn">返回10</button>
    3. <script>
    4. var btn = document.getElementById("btn")
    5. btn.onclick = function(){
    6. /* 回退到前一个页面 */
    7. history.back()
    8. }
    9. </script>
    10. </body>

    image.png
    image.png

    4-7.获取屏幕宽高

  • screen.availWidth —获取屏幕宽度

  • screen.availHeight —获取屏幕高度
  • document.documentElement.clientWidth —获取视窗宽度
  • document.documentElement.clientHeight —获取视窗高度

    1. <body>
    2. <script>
    3. /* 获取屏幕宽高 */
    4. var sw = screen.availWidth;
    5. console.log(sw);
    6. var sh = screen.availHeight;
    7. console.log(sh);
    8. var vh = document.documentElement.clientHeight;
    9. /* 可视区域的视窗高度 */
    10. console.log(vh)
    11. </script>
    12. </body>

    image.png

    4-8.实现上拉刷新的原理

  • 如何判断滚动条到达底部?

  • 滚动移动的距离+视窗高度vh = body的高度 —代表着滚动条到达最底部

  • body高度:var bh = document.body.clientHeight
  • 视窗高度:var vh = document.documentElement.clientHeight;
  • 滚动条距离顶部距离:window.onscroll = function(){

var scrollTop = window.scrollY;
console.log(scrollTop);
}

  1. <style>
  2. *{
  3. margin: 0;padding: 0
  4. }
  5. body{
  6. height: 2000px;
  7. }
  8. div{
  9. width: 300px;
  10. height: 300px;
  11. background: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div></div>
  17. <script>
  18. var bh = document.body.clientHeight;
  19. var vh = document.documentElement.clientHeight;
  20. console.log(bh);
  21. console.log(vh);
  22. /* 滚动移动的距离+视窗高度vh = body的高度
  23. 代表着滚动条到最底部
  24. */
  25. window.onscroll = function(){
  26. var scrollTop = window.scrollY;
  27. console.log(scrollTop);
  28. }
  29. </script>
  30. </body>

image.png
image.png

4-9.跳转传值location

1、设置键盘事件点击键盘的enter,获取input的值
2、将这个值传递给另一个页面 location.href = (传递的值为ASCII码)
通过decodeURIComponent(“”)转译

  1. <input type="text" id="app">
  2. <script>
  3. /* 1、点击键盘的enter,获取input的值 */
  4. var app = document.getElementById("app")
  5. app.onkeydown = function(event){
  6. if(event.keyCode == 13){
  7. console.log(this.value)
  8. /* 2、将这个值传递给另一个页面 */
  9. location.href = "14.html?s="+this.value;
  10. }
  11. }
  12. </script>
  1. <script>
  2. console.log(location.search);
  3. console.log(decodeURIComponent("%E4%BD%A0%E5%A5%BD"));
  4. </script>

image.pngimage.png

5、函数

5-1.创建函数

  • 1、以直接量的方式创建(推荐)

tips:在函数前后调用都是可以的

  • 2、以变量的方式创建30

tips:只能在函数之后调用

  1. <script>
  2. /* 1、以直接量的方式创建(推荐)
  3. tips:在函数前后调用都是可以的
  4. */
  5. go();
  6. function go(){
  7. console.log("hello world")
  8. }
  9. /* 2、以变量的方式创建
  10. tips:只能在函数之后调用
  11. */
  12. var b = function(){
  13. console.log("good")
  14. }
  15. b();
  16. </script>

image.png

5-2.return函数

  • 函数的返回值
  • 函数的执行结果

tips:

  • 1、函数调用后,return后面的语句不会执行
  • 2、函数遇到return语句会立即返回停止,退出
  • 3、return语句的作用,将函数内部的值返回给外部

    1. <script>
    2. /*
    3. 函数的返回值
    4. 函数的执行结果
    5. tips:
    6. 1、函数调用后,return后面的语句不会执行
    7. 2、函数遇到return语句会立即返回停止,退出
    8. 3、return语句的作用,将函数内部的值返回给外部
    9. */
    10. function go(){
    11. return 10;
    12. console.log("hello world");
    13. }
    14. var res = go();
    15. console.log(res);
    16. </script>

    image.png

    5-3.函数的参数

  • 函数的参数是局部变量(未设值的参数为undefined)

tips:JS传不定参

  1. <script>
  2. function go(a,b){
  3. /* 函数的参数是局部变量 */
  4. console.log(a);
  5. console.log(b);
  6. }
  7. /* tips:JS传不定参 */
  8. go();
  9. go(10);
  10. go(10,20);
  11. </script>

image.png

5-4.重载

  • 使用function声明的方法,本质上是window方法

后定义的函数覆盖前定义的函数

  1. <script>
  2. /* 使用function声明的方法,本质上是window方法 */
  3. function go(a){
  4. console.log(a)
  5. }
  6. function go(a,b){
  7. console.log(a);
  8. console.log(a+b);
  9. }
  10. window.go(10);
  11. </script>

image.png

  1. <script>
  2. /* 使用function声明的方法,本质上是window方法 */
  3. var go = function(a){
  4. console.log(a)
  5. }
  6. var go = function(a,b){
  7. console.log(a);
  8. console.log(a+b);
  9. }
  10. go(10);
  11. </script>

image.png

5-5.JavaScript实现重载

  • 重载:函数名相同,参数不同,根据传入的参数动态的决定调用哪个方法

JS不支持重载

  • 要想JS支持 arguments对象

JS传不定参,函数使用arguments对象管理函数内部的参数

  1. <script>
  2. /* 重载:函数名相同,参数不同,根据传入的参数动态的决定调用哪个方法
  3. JS不支持重载
  4. */
  5. /* 要想JS支持 arguments对象
  6. JS传不定参,函数使用arguments对象管理函数内部的参数
  7. */
  8. function go(){
  9. console.log(arguments);
  10. }
  11. go(10,20)
  12. </script>

image.png

  • 通过if语句判断arguments管理的函数内部的参数个数,决定输出多少值实现动态伪重载

    1. <script>
    2. /*
    3. 一个参数
    4. 两个参数
    5. */
    6. function go(){
    7. if(arguments.length == 1){
    8. console.log(arguments[0]);
    9. }else if(arguments.length == 2){
    10. console.log(arguments[0]);
    11. console.log(arguments[0]+arguments[1]);
    12. }
    13. }
    14. go(10);
    15. go(10,20);
    16. </script>

    image.png

    5-6.函数对象

    函数对象 —函数作为对象的方法
    this关键字指向问题
    1、onclick等事件 —谁执行事件,this指向谁
    2、谁执行方法,this指向谁(哪个对象执行方法,this指向那个对象)

    1. <script>
    2. /* 函数对象
    3. 函数作为对象的方法
    4. */
    5. var obj = {
    6. name:"html",
    7. sayName:function(){
    8. console.log(this.name)
    9. }
    10. }
    11. /* 函数使对象的一个方法 */
    12. obj.sayName();
    13. /*
    14. this关键字指向问题
    15. 1、onclick 谁执行事件,this指向谁
    16. 2、谁执行方法,this指向谁
    17. */
    18. </script>

    image.png

    !5-7.回调函数

    回调函数 —就是函数作为参数传递给另一个函数!
    使用场景:异步问题

    1. <script>
    2. /* 回调函数
    3. 就是函数作为参数传递给另一个函数
    4. 使用场景:异步问题
    5. */
    6. function show(a){
    7. console.log(a);
    8. }
    9. function go(callback){
    10. var b = 10;
    11. callback(b);
    12. }
    13. go(show);
    14. /*
    15. callback = show
    16. */
    17. </script>

    image.png
    简写:
    执行go()函数:
    1.将function(a)作为go函数中的callback参数
    2.定义b=20
    3.callback(b) = function(b)
    4.function(b)为输出b
    5.go函数执行完毕,输出b=20

    1. <script>
    2. function go(callback){
    3. var b =20;
    4. callback(b);
    5. }
    6. go(function(a){
    7. console.log(a);
    8. })
    9. </script>

    image.png

    5-8.es6写法

    es5定义函数
    var 函数名 = function(参数){
    函数体
    }
    es6定义函数
    var 函数名=(参数)=>{
    函数体
    }

    1. <script>
    2. /* es5 */
    3. var shou = function(a){
    4. console.log(a);
    5. }
    6. /* es6 箭头
    7. 特点:省略了function关键字
    8. */
    9. var go=(a)=>{
    10. console.log(a);
    11. }
    12. go(30);
    13. </script>

    image.png
    简写:

  • tips:如果参数只有一个可以省略小括号,输出语句只有一段可以省略大括号

    1. <script>
    2. var go =(a)=>{
    3. console.log(a);
    4. }
    5. /*简写
    6. tips:如果参数只有一个可以省略小括号,输出语句只有一段可以省略大括号
    7. */
    8. var show = (a)=>console.log(a);
    9. go(10);
    10. show(10);
    11. </script>

    image.png

  • 如果输出语句只有一段return语句,可以省略return

    1. <script>
    2. var go = (a)=>{
    3. return a;
    4. }
    5. /* 如果输出语句只有一段return语句,可以省略return */
    6. var show = (a)=>a;
    7. console.log(show(10))
    8. </script>

    6、数组

    6-1.定义一个数组

    var 数组名 = []; —.length 获取数组长度

    1. <!-- 数组
    2. 增,删,改,查,遍历
    3. -->
    4. <script>
    5. var arr = [1,2,3];
    6. /* length */
    7. console.log(arr.length);
    8. console.log(arr[0]);
    9. </script>

    image.png

    6-2.数组的后增加

    push 向数组的后面增加
    特点:
    1、可以增加多个值
    2、增加数组时是原数组变为二维数组
    使用场景:加载更多 上拉刷新

    1. <script>
    2. var arr = [1,2,3];
    3. /*
    4. push 向数组的后面增加
    5. 特点:
    6. 1、可以增加多个值
    7. 使用场景:加载更多 上拉刷新
    8. */
    9. arr.push(4);
    10. arr.push(5,6);
    11. arr.push([7,8,9])
    12. console.log(arr);
    13. </script>

    image.png

    6-3.数组的前增加

    unshift 从前增加
    使用场景:历史搜索 下拉刷新

    1. <script>
    2. /*
    3. 从前增加
    4. unshift
    5. 使用场景:历史搜索 下拉刷新
    6. */
    7. var arr = [1,2,3]
    8. arr.unshift(0);
    9. console.log(arr);
    10. </script>

    image.png

    6-4.获取数组中值的下标,判断数组中是否重复

    .indexOf 可以判断数组中值的下标
    -1 —数组中没有这个值

    1. <script>
    2. /*
    3. indexOf 可以判断数组中值的下标
    4. -1 --数组中没有这个值
    5. */
    6. var arr = [1,2,3,4];
    7. console.log(arr.indexOf(4));
    8. console.log(arr.indexOf(3));
    9. console.log(arr.indexOf(8));
    10. </script>

    image.png

    6-5.concat函数

    concat
    特点:
    1、支持添加多个值
    2、支持数组
    3、不会改变数组原本的结构

    1. <script>
    2. /*concat
    3. 特点:
    4. 1、支持添加多个值
    5. 2、支持数组
    6. 3、不会改变数组原本的结构
    7. */
    8. var arr=[1,2,3];
    9. var b = arr.concat([4,5]);
    10. console.log(b);
    11. console.log(arr);
    12. var test = [6,7,8];
    13. test.push([9,10]);
    14. console.log(test);
    15. </script>

    image.png

    !6-6.设置简单的搜索框历史搜索

    1.定义数组historys=[]存储历史搜索
    2.if判断键盘事件的值,为13则是enter键
    3.在判断输入的值不为空且.indexOf值为-1则表示历史数组中没有重复值
    4.将搜索框的值从前增加至历史数组中 输出数组
    5.否则则提醒弹窗 “输入的字段不能是是空字符或相同字段”

    1. <input type="text" id="input">
    2. <h2>历史搜索</h2>
    3. <div id="app">
    4. </div>
    5. <script>
    6. var input = document.getElementById("input");
    7. var historys = []
    8. input.onkeydown = function(event){
    9. if(event.keyCode == 13){
    10. // console.log(this.value);
    11. if(this.value!="" && historys.indexOf(this.value) == -1){
    12. historys.unshift(this.value);
    13. console.log(historys);
    14. }else{
    15. alert("输入的字段不能是是空字符或相同字段")
    16. }
    17. }
    18. }
    19. </script>

    image.png
    优化:
    1、获取输入框的值
    2、只要数组中没有的值同时字符不能空,才向数组添加

  • 再定义创建P对象,将搜索框的值付给P对象

  • 利用.prepeng从前添加P元素
  • 每次将搜索框中的值赋给history数组时,清空搜索框

    1. <input type="text" id="input">
    2. <h2>历史搜索</h2>
    3. <div id="app">
    4. </div>
    5. <script>
    6. var input = document.getElementById("input");
    7. var app = document.getElementById("app");
    8. var historys = []
    9. /* 1、获取输入框的值 */
    10. input.onkeydown = function(event){
    11. if(event.keyCode == 13){
    12. // console.log(this.value);
    13. /* 2、只要数组中没有的值同时字符不能空,才向数组添加 */
    14. if(this.value!="" && historys.indexOf(this.value) == -1){
    15. var p =document.createElement("p");
    16. p.innerHTML = this.value;
    17. app.prepend(p);
    18. historys.unshift(this.value);
    19. this.value = "";
    20. console.log(historys);
    21. }else{
    22. alert("输入的字段不能是是空字符或相同字段")
    23. }
    24. }
    25. }
    26. </script>

    image.png