JavaScript


JavaScript 详解

1. JavaScript介绍

  • 虽然是java作为前缀,但java和javascript的关系,就像老婆和老婆饼之间的关系,没有一毛钱关
  • 系!
  • 网景公司在Netscape2.0首先推出了JavaScript
  • JavaScript 的正式名称是 “ECMAScript”,此标准由 ECMA 组织发展和维护,简称“js”
  • JavaScript 是一种网页编程技术,用来向 HTML 页面添加交互行为
  • JavaScript 是一种基于对象和事件驱动的解释性脚本语言,直接嵌入 HTML 页面,由浏览器解释执行代码,不进行预编译。

1.1 js的特点

  • 可以使用任何文本编辑工具编写,只需要浏览器就可以执行程序 (后面会学习不用浏览器也能运行)
  • 解释执行:事先不编译,逐行执行
  • 基于对象:内置大量现成对象
  • 适宜:
    • 客户端数据计算
    • 客户端表单合法性验证
    • 浏览器事件的触发
    • 网页特殊显示效果制作

1.2 js的组成

  • ECMAScript:定义核心语法,关键字,运算符,数据类型等系列标准
  • DOM:文档对象模型,将一个html页面的所有节点看成是一个一个的对象。更有层次感的管理每
  • 一个节点。
  • BOM:浏览器对象模型,是对浏览器窗口进行访问和操作。使用 BOM,开发者可以移动窗口、改
  • 变状态栏中的文本以及执行其他与页面内容不直接相关的动作。使 BOM 独树一帜且又常常令人怀
  • 疑的地方在于,它只是 JavaScript 的一个部分,没有任何相关的准。
    • 弹出新的浏览器窗口
    • 移动、关闭浏览器窗口以及调整窗口大小
    • 提供 Web 浏览器详细信息的定位对象
    • 提供用户屏幕分辨率详细信息的屏幕对象
    • 对 cookie 的支持
    • IE 扩展了 BOM,加入了 ActiveXObject 类,可以通过 JavaScript 实例化 ActiveX对象,进而
    • 实现ajax局部刷新技术

2. HTML与javaScript结合方式

使用js的三种方式

2.1.行内脚本

  • 1. 点击按钮(触发)
  • 2. 弹框(具体的操作)

2.2.内部脚本

1. 使用
2. 标准是写在head和body之间(脖子位置),但其实只要写在html文件内部就可以,无论什么位置
外,

内部,都可以。


  

2.3.外部脚本

1. 在项目根目录下创建一个目录js
2. 在js目录中创建一个文件,后缀名是.js
3. 在html页面中,使用

以上使用脚本的三种方式的优先级,谁在上,谁先执行。因为是解释性语言。

3. JavaScript的使用

3.1 变量

  • 因为js是弱类型语言,所以,在定义变量的时候,所有的数据类型都是var
  • 声明变量: var x ; var x,y;
  • 数值类型:number
    • 不区分整型数值和浮点型数值
    • 所有数字都采用 64 位浮点格式存储,类似于double 格式
  • 字符串:string
    • 首尾由单引号或双引号括起
  • 布尔类型:
    • 仅有两个值:true和false也代表1和0
    • 实际运算中true=1,false=0

3.1.1 自动类型转换

数字 + 字符串:数字转换为字符串 10+’a’ -> 10a
数字 + 布尔值:true转换为1,false转换为0 true+5->6
字符串 + 布尔值:布尔值转换为字符串true或false true+’a’->truea
布尔值 + 布尔值:布尔值转换为数值1或0 true+true->2

3.1.2 数据类型转换函数

  • parseInt:强制转换成整数
    • 如果不能转换,则返回 NaN (NaN 属性是代表非数字值的特殊值。该属性用于指示某个值不是数字)
    • 例如:parseInt("6.32")=6
  • parseFloat:强制转换成浮点数
    • 如果不能转换,则返回 NaN
    • 例如:parseFloat("6.32")=6.32
  • typeof:查询数值当前类型,返回 string / number / boolean / object
    • 例如:typeof("test"+3)=="string"

3.1.3 null 与 undefined

  • null 在程序中代表“无值”或者“无对象”
    • 可以通过给一个变量赋值 null 来清除变量的内容
  • undefined
    • 声明了变量但从未赋值或者对象属性不存在

3.1.4 算术运算

  • 加(+)、 减(-)、 乘( * ) 、除( / ) 、余数( % )
    • - 可以表示减号,也可以表示负号,如:x = -y
    • +可以表示加法,也可以用于字符串的连接
  • 递增( ++ ) 、递减( -- )
    • i++ 相当于 i=i+1
    • i-- 相当于 i=i-1

3.1.5 关系运算

  • 严格相等:===
    • 类型相同
    • 数值相同
  • 非严格相等:!==
var a = "10";
var b = 10;
if (a == b)
  alert("equal");
if (a === b)
  alert("same");

3.1.6 逻辑运算

  • 逻辑非(!)、逻辑与(&&)、逻辑或(||)
  • 逻辑运算的操作数均为 boolean 表达式
  • 我要吃两碗拉面或者10个包子才能吃饱!问题是,我只吃两碗面,饱了! 我只吃10个包子,饱了
  • 我要吃两碗拉面并且10个包子才能吃饱!问题是,我只吃两碗面,没饱! 我只吃10个包子,没饱,

3.1.7 控制语句

if(关系表达式) {
  // 语句块 1
}else {
  // 语句块 2
}
if (表达式1) {
    // 语句1;
}else if (表达式2){
    // 语句2;
}else if (表达式3){
    // 语句3;
} else{
    // 语句4;
}
switch (表达式) {
  case 值1:
    // 语句1;
    break;
  case 值2:
    // 语句2;
    break;
  default:
    // 语句4;
}
for (var i=1 ; i<=5 ; i++){
  alert(i);
}
while (条件){
    // 语句1;
    ...
}

3.2 常用字符串API

  • length:获取字符串的长度(字符串中字符的个数) 属性,没有小括号
  • var str = "hello";
    console.log( str.length );
  • toUpperCase/toLowerCase :转大小写
  • var name = "AngierSun";
    console.log( "大写:"+name.toUpperCase() );
    console.log( "小写:"+name.toLowerCase() );
  • charAt(下标) : 返回某个下标上的字符
  • var str1 = "javascript网页教程";
    var str2 = str1.charAt(12); // 下标12上的字符
    console.log(str2); //
    var str3 = str1.charCodeAt(12);
    console.log(str3); //25945:(汉字“教”在unicode编码中的编号)
  • indexof(字符):查找字符串中字符出现的首次下标
  • lastIndexof(字符):查找字符串中字符最后一次出现的下标
  • var str1 = "javascript网页教程";
    var str2 = str1.indexOf("a");
    console.log(str2); // 1 , a字符在str1中第一次出现的下标
    var str3 = str1.lastIndexOf("a"); //3,a字符在str1中最后一次出现的下标
    console.log(str3);
  • substring(开始,结束):截取字符串中一部分(结束是不包含的)
  • var str1 = "abcdefgh";
    var str2 = str1.substring(2,4);
    console.log(str2); //cd,从2开始(包含),4结束(不包含)
  • replace(旧的,新的):将字符串中的旧字符替换成新字符
  • var str1 = "abcde";
    var str2 = str1.replace("cd","XXX");
    console.log(str2); // abXXXe,将str1中的cd替换成XXX
  • split(分割的节点):一个字符串切割成N个小字符串,所以返回的是数组类型
  • var str1 = "一,二,三,四,五";
    var arr = str1.split(","); // 将str1 以逗号进行分割,分割成N份,所以返回的结果一定
    是数组结构
    console.log( "共分割成:"+arr.length+"份" );
    console.log( "第三份是:" + arr[2] ); //

3.3 数组

3.3.1 创建数组

var arr1 = new Array();

3.3.2 初始化数组的三种方式

// 第一种
var arr1 = new Array();
arr1[0] = 110;
arr1[1] = 119;
arr1[2] = 120;
// 第二种
var arr1 = new Array(10,"a",true);
// 第三种
var arr1 = [10,"a",true];
for (var i = 0; i < arr1.length; i++) {
  console.log(arr1[i]);
}

3.3.3 数组的常用方法

  • tostring():将数组转换成字符串
  • var arr = [1,2,3,4];
    console.log("类型为:" + typeof( arr ) );
    var str = arr.toString(); // 将数组转换成字符串
    console.log( str +",类型为:" + typeof( str ) );
  • join(连接符号):将数组中的每个元素用连接符号连接成一个新的字符串。
  • var arr = [1,2,3,4];
    var str = arr.join("-"); // 将数组中每个元素用-进行连接,并形成一个全新的字符串
    console.log( str +",类型为:" + typeof( str ) );
  • concat(新元素):将原来的数组连接新元素,原数组不变。
  • var arr = [1,2,3,4];
    var arrnew = arr.concat(5,6); // 在arr数组的后面添加新的元素,形成一个新数组,但是
    原数组是不变的
    console.log( arrnew +",类型为:" + typeof( arrnew ) );
    console.log("原数组:" + arr);
  • slice(开始,结束):在数组中提取一部分,形成新的数组。
    • 1,2,3,4,5 slice(2,4) 结果:3,4
    • var arr = ['a','b','c','d','e','f','g','h'];
      var arrnew = arr.slice( 2,4 ); // 在arr数组中截取,从2开始(包含),4结束(不包
      含)
      console.log( arrnew ); // cd
  • reverse():数组的反转(倒序)
  • var arr = [31,12,111,444];
    console.log( arr.toString() );
    arr.reverse(); // 将数组中的元素倒置
    console.log( arr.toString() );
  • sort():数组排序
    • arr.sort() 字符排序
      • var arr = [31,12,111,444];
        arr.sort(); // 字符排序(不会按照字面量的大小)
        console.log( arr );
    • arr.sort(func) 数值排序
      • var arr = [31,12,111,444];
        arr.sort( laosun ); // 数字排序(会按照字面量的大小)
        console.log( arr );
        // 定义排序函数
        function laosun(a,b){
          return a-b;
        }

3.4 Math数学对象

  • Math 对象用于执行数学任务
  • 没有构造函数 Math()
  • 无需创建,直接把 Math 作为对象使用就可以调用其所有属性和方法

// 返回0-9之间任意一个随机数字
var i = Math.random() * 10;
var j = Math.floor(i);
console.log(j);

3.5 Number对象

Number.fixed(2); 自带四舍五入技能

var n = new Number( 12.345 );
var n1 = n.toFixed(2); // 12.35,固定两位小数,第三位小数四舍五入
console.log( n1 );
var x = new Number( 12.3 );
var n2 = x.toFixed(2); // 12.30,固定两位小数,位数不够,0来补齐
console.log( n2 );


3.6 正则表达式

对字符串执行模式匹配的强大工具

var reg1 = /^\d{3,6}$/; // 匹配纯数字3-6个
var reg2 = new RegExp(“^\\d{3,6}$");

// 方式1
var age = "18"; // 判断:1-3位纯数字
var reg = /^\d{1,3}$/; // 以/^开始,中间写正则内容,以$/结束
var b = reg.test(age); // 验证age变量的是否符合reg的匹配
if (b == true) {
  console.log("验证通过!");
} else {
  console.log("格式错误");
}
// 方式2
var name = "abc123"; // 大小写字母和数字的组合(特殊字符不能出现), 5~8位
var reg = new RegExp("^[a-zA-Z0-9]{5,8}$"); // 以^开始,中间写正则内容,以$结束
if (reg.test(name)) {
  console.log("验证通过!");
} else {
  console.log("格式错误");
}

3.7 日期对象

var time = new Date();
console.log( time ); // Tue Jul 14 2020 11:09:46 GMT+0800 (中国标准时间)
var year = time.getFullYear(); // 年份
var month = time.getMonth() + 1; //月份从0开始,11结束,所以国内习惯要+1
var day = time.getDate(); // 几号
var hour = time.getHours(); // 几点
var mm = time.getMinutes(); // 分钟
var s = time.getSeconds(); //
var ms = time.getMilliseconds(); // 毫秒, 1000毫秒 = 1秒
var timestr = year+"年"+month+"月"+day+"号 "+hour+"点"+mm+"分"+s+"秒"+ms+"毫秒";
console.log( timestr );

3.8 函数

  • 使用关键字 function 定义函数
    • function 函数名( 形参列表 ){
          // 函数体
          return 返回值;
      }
  • 函数声明后不会立即执行,会在我们需要的时候调用到。
  • 注意:
    • 形参:一定不要带数据类型
    • 分号是用来分隔可执行JavaScript语句。 由于函数声明不是一个可执行语句,所以不以分号结束。

3.8.1 无返回值

function qiuhe(a, b) {
  var he = a + b;
  console.log("两数之和:" + he);
}
qiuhe(3,4);

3.8.2 有返回值

function qiuhe(a, b) {
  var he = a + b;
  return "两数之和:" + he;
}
var s = qiuhe(3,4);
console.log( s );

3.8.3 参数对象

在函数内部,调用参数列表的属性

function func(a,b,c){
    console.log( arguments.length ); // 获得参数的个数
    console.log( arguments[1] ); // 获得下标为1的参数
}

3.8.4 构造函数

函数同样可以通过内置的 JavaScript 函数构造器(Function())定义

var myFunction = new Function("a", "b", "return a * b");
var x = myFunction(4, 3);
console.log(x);

注: 上述函数以分号结尾,因为它是一个执行语句。

3.8.5 匿名函数

没有名称的函数

var fn = function(a, b) {// 没有名字的函数,应该用一个变量来接收
    return a * 10 + b;
};
console.log( fn(3, 4) );

3.8.6 全局函数

  • isNaN:检查其参数是否是非数字值
    • console.log( isNaN( 123 ) ); // 数字,false
      console.log( isNaN( "hello" ) ); // 非数字,true
      console.log( isNaN( 4-1 ) ); // 数字,false
      console.log( isNaN( 123 ) ); // 数字,false
      console.log( isNaN( -10 ) ); // 数字,false
      console.log( isNaN( "123" ) ); // 数字,false
      console.log( isNaN( "1a23" ) ); // 非数字,true
  • eval:用来转换字符串中的运算
    • var str = "1+3";
      console.log( str ); // 1+3 , +会认定为一种字符符号而已,没有加法的作用
      console.log( eval( str ) ); // 让字符串中的运算符号生效
  • encodeURI 与 decodeURI
    • var name = "拉勾网";
      console.log( "转码前:" + name );
      name = encodeURI(name);
      console.log( "转码后:" + name );
      name = decodeURI(name);
      console.log( "解码后:" + name );

3.8.7 闭包

1、闭包的概念:指有权访问另一个函数作用域中的变量的函数,一般情况就是在一个函数中包含另一
个函数。

2、闭包的作用:访问函数内部变量、保持函数在环境中一直存在,不会被垃圾回收机制处理;

简单地说:就是在函数的局部范围内声明一个封闭的环境,此环境不会被垃圾回收探测到。保证了数据
的安全唯一性

想了解闭包,首先要了解什么是全局变量,什么是局部变量

a = 10; // 全局变量,声明的时候可以不使用var
function test1(){
  b = 20; // 不适用var声明的变量,就是全局变量
  var c = 30; // 用var声明,并且在函数的内部。这样的变量叫做局部变量,有效范围只能
  //在其声明的函数内部
  console.log(c);
}
function test2(){
  console.log(c); //c is not defined (c变量没有定义)
}
test1();
test2();

需求:统计方法执行了多少次

var count = 0; // 总次数
function test1(){
  count++; // 自增+1
}
test1();
test1();
test1();
console.log( count );

谁都可以访问count,所以count变量并不安全,因为是全局变量。

如何才能安全呢?将count声明为局部变量

function test1(){
  var count = 0; //局部变量
  return count++; //外部无法访问count,只能通过return才能将count变量返回,并输
  //出
}
test1();
test1();
test1();

console.log( test1() );
// 每次调用方法,首先就是将变量还原为0

结果一直是0,因为每次调用test1(),方法体的第一句代码就是还原,无论曾经的值是多少。

突发奇想,如果在test1()函数里面,再嵌套一个函数,js是支持函数嵌套的

  function test1(){
    var count = 0; //局部变量
    function jia(){
      return count++;
    }
    jia();
    return count;
  }
test1();
test1();
test1();
console.log( test1() );
// 每次调用方法,首先就是将变量还原为0

如果每次只调用test1()里面的jia()就好了。ok,闭包帮你解决此问题!

function test1(){
  var count = 0; //局部变量
  function jia(){
    return count+=1;
  }
  return jia;
}
var fn = test1(); // fn => function jia(){return count+=1; }
fn();
fn();
console.log( fn() ); // 每次调用方法,首先就是将变量还原为0


闭包是一种保护私有变量的机制,在函数执行时形成私有的作用域,保护里面的私有变量不 受外界干
扰。

直观的说就是形成一个不销毁的栈环境。

  • 闭包的优点: 方便调用上下文中声明的局部变量 逻辑紧密,可以在一个函数中再创建个函数,避免了传参的问题
  • 闭包的缺点: 因为使用闭包,可以使函数在执行完后不被销毁,保留在内存中,如果大量使用闭包就会造 成内存泄露,内存消耗很大

3.9 弹框输出

  • 普通弹框 alert("hello,拉勾");
  • 控制台日志输出 console.log("谷歌浏览器按F12进入控制台");
  • 页面输出 document.write("

    我爱你中国

    "); 将

    元素输出到中

  • 确认框 confirm("确定删除吗?");
    • var b = confirm("确定删除吗?");
      if(b){
          document.write( "

      删除成功!

      " ); }else{ document.write( "

      你取消了操作

      " ); }
  • 输入框 prompt("请输入姓名:");
    • var name = prompt("请输入你的名字:");
      document.write( "

      大名:"+name+"!

      " );

4.DOM 操作

  • 在一个html页面中,会使用很多标签来规划制作页面。
  • 每个标签都有它存在的意义,如果我们想要动态的修改某个标签的值。那我们就需要在页面中查找到这个标签元素
  • 如何查找到这个元素是个难题,W3C组织的工程师们,突然看到了一棵大树。我要是想找到某一片叶子,应该怎么做?
  • “顺藤摸瓜”,主树干有分支,每个分支还有许多小分支,只要把这个分支的结构整理清楚,任何一片叶子都不是难事了
  • 叶子和大树的一些启发,工程师们开会讨论就定了这个理论“文档对象模型”,
  • 文档对象模型,就是将页面中所有的标签元素都看成是一个对象(一片叶子),主树干定义为根节点(根元素),所有的标签都是从根元素延伸出去的,摸清结构,找到某个标签就不再困难了
    • 在节点树中,顶端节点就是根节点(root)
    • 每个节点都有父节点(除了根节点)
    • 任何一个节点都可以拥有任意数量的子节点
    • 同胞是拥有相同父节点的节点
  • <html>
    <head>
      <meta charset="utf-8">
      <title>DOM 教程title>
    head>
    <body>
      <h1>第一节:HTML DOMh1>
      <p>Hello world!p>
    body>
    html>

从上面的 HTML 中:

  • 节点没有父节点;它是根节点
  • 和 的父节点是 节点
  • 文本节点 "Hello world!" 的父节点是

    节点

并且:

  • 节点拥有两个子节点: 和
  • 节点拥有两个子节点: 节点</li> <li><title> 节点也拥有一个子节点:文本节点 "DOM 教程"<h1> 和 <p> 节点是同胞节点,同时也是<body> 的子节点</li> </ul> <p>并且:</p> <ul> <li><head> 元素是 <html> 元素的首个子节点</li> <li><body> 元素是 <html> 元素的最后一个子节点</li> <li><h1> 元素是 <body> 元素的首个子节点</li> <li><p> 元素是 <body> 元素的最后一个子节点</li> </ul> <p>js为我们提供了很多种方法来实现在页面找查找某个元素节点</p> <h3>4.1 DOM访问</h3> <ul> <li>getElementById:通过id属性获得元素节点对象 <ul> <li>案例:当帐号为空时,阻止表单提交</li> </ul> </li> </ul> <pre><body>   <form action="xxx" onsubmit="return login()">     <p>帐号:<input id="username"/></p>     <p>电话:<input id="phone"/></p>     <p><button>登录</button></p>   </form>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> login() {       </span><span style="color: rgba(0, 0, 255, 1)">var</span> name = document.getElementById("username"<span style="color: rgba(0, 0, 0, 1)">).value ;       </span><span style="color: rgba(0, 0, 255, 1)">if</span>(name == ""<span style="color: rgba(0, 0, 0, 1)">){         alert(</span>"帐号不能为空!"<span style="color: rgba(0, 0, 0, 1)">);         </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 阻止表单的提交</span>       <span style="color: rgba(0, 0, 0, 1)">}         </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 放行,让表单提交</span>       <span style="color: rgba(0, 0, 0, 1)">}   </span></script> </body></pre> <ul> <li>getElementsByName:通过name属性获得元素节点对象集 <ul> <li>案例:购物车全选效果</li> </ul> </li> </ul> <pre><body>   <h2>我的购物车</h2>   <table border="1" cellspacing="0">     <tr>       <td><input type="checkbox" onchange="quan(this)" /> 全选</td>       <td>名称</td>       <td>单价</td>     </tr>     <tr>       <td><input type="checkbox" name="one" />1</td>       <td>功能性饮料-尖叫</td>       <td>4.0</td>     </tr>     <tr>       <td><input type="checkbox" name="one" />2</td>       <td>火腿肠</td>       <td>2.0</td>     </tr>     <tr>       <td><input type="checkbox" name="one" />3</td>       <td>包子</td>       <td>1.5</td>     </tr>   </table>   <p>     <button>提交订单</button>   </p>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> quan(all) {       </span><span style="color: rgba(0, 0, 255, 1)">var</span> arr = document.getElementsByName("one"<span style="color: rgba(0, 0, 0, 1)">);       </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i < arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {         arr[i].checked </span>= all.checked; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将全选框的状态,赋值给每一个复选框</span>       <span style="color: rgba(0, 0, 0, 1)">}     }   </span></script> </body></pre> <ul> <li>getElementsByTagName:通过标签名称获得元素节点对象集 <ul> <li>案例:表格隔行变色</li> </ul> </li> </ul> <pre><body>   <table border="1" cellspacing="0">     <tr>       <td><input type="checkbox" onchange="quan(this)" /> 全选</td>       <td>名称</td>       <td>单价</td>     </tr>     <tr>       <td><input type="checkbox" name="one" />1</td>       <td>功能性饮料-尖叫</td>       <td>4.0</td>     </tr>     <tr>       <td><input type="checkbox" name="one" />2</td>       <td>火腿肠</td>       <td>2.0</td>     </tr>     <tr>       <td><input type="checkbox" name="one" />3</td>       <td>包子</td>       <td>1.5</td>   </table>   <script>     <span style="color: rgba(0, 0, 255, 1)">var</span> rows = document.getElementsByTagName("tr"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过标签名获得元素对象</span>     <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i < rows.length; i++<span style="color: rgba(0, 0, 0, 1)">) {       </span><span style="color: rgba(0, 0, 255, 1)">if</span>(i % 2 == 1){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 奇数</span>         rows[i].style.backgroundColor = "pink"<span style="color: rgba(0, 0, 0, 1)">;       }     }   </span></script> </body></pre> <h3>4.2 DOM修改</h3> <ul> <li>修改 HTML DOM 意味着许多不同的方面: <ul> <li>改变 HTML 内容</li> <li>改变 CSS 样式</li> <li>改变 HTML 属性</li> <li>创建新的 HTML 元素</li> <li>删除已有的 HTML 元素</li> <li>改变事件(处理程序)</li> </ul> </li> </ul> <p>1. 改变一个 <h2> 元素的 HTML 内容 :</p> <pre><body> <button onclick="test()">点我试试</button> <script> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> test(){ document.getElementById(</span>"hello").innerHTML = "走哇,喝点去~!"<span style="color: rgba(0, 0, 0, 1)">;      }   </span></script>   <h2 id="hello">你好!</h2> </body> </pre> <p>2. 改变一个<h2>的 HTML 样式</p> <pre><body>   <button onclick="chou()">你瞅啥</button>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> chou(){       document.getElementById(</span>"hello").style.color = "red"<span style="color: rgba(0, 0, 0, 1)">;       document.getElementById(</span>"hello").style.fontFamily = "华文彩云"<span style="color: rgba(0, 0, 0, 1)">;     }   </span></script>   <h2 id="hello">你好!</h2> </body></pre> <h4>4.2.1 添加节点</h4> <ul> <li>点击按钮,在页面中创建一张图片</li> </ul> <pre><body>   <button onclick="add()">添加</button>   <div></div>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> add(){       </span><span style="color: rgba(0, 0, 255, 1)">var</span> img = document.createElement("img"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> <img></span>       img.setAttribute("src","../lagou-html/img/cat.gif"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> <img</span>       src="../lagou-html/img/cat.gif"><span style="color: rgba(0, 0, 0, 1)">       img.setAttribute(</span>"title","小猫咪"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> <img src="../lagouhtml/</span>       img/cat.gif" title="小猫咪">       img.setAttribute("id","cat"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> <img src="../lagouhtml/</span>       img/cat.gif" title="小猫咪" id="cat">       <span style="color: rgba(0, 0, 255, 1)">var</span> divs = document.getElementsByTagName("div"<span style="color: rgba(0, 0, 0, 1)">);       divs[</span>0<span style="color: rgba(0, 0, 0, 1)">].appendChild(img);     }   </span></script> </body></pre> <h4>4.2.2 删除节点</h4> <ul> <li>点击按钮,把上面刚创建的图片从页面上删除</li> </ul> <pre><body>   <button onclick="add()">添加</button>   <button onclick="del()">删除</button>   <div>   </div>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> add(){       。。。略。。。     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> del(){       </span><span style="color: rgba(0, 0, 255, 1)">var</span> img = document.getElementById("cat"<span style="color: rgba(0, 0, 0, 1)">);       img.parentNode.removeChild(img); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 必须通过父节点,才能删除子节点</span>     <span style="color: rgba(0, 0, 0, 1)">}   </span></script> </body></pre> <h4>4.2.3 替换节点</h4> <ul> <li>点击按钮,把上面刚创建的图片替换成另一张</li> </ul> <pre><body>   <button onclick="add()">添加</button>   <button onclick="del()">删除</button>   <button onclick="rep()">替换</button>   <div>   </div>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> add(){       。。。略。。。     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> del(){       。。。略。。。     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> rep(){       </span><span style="color: rgba(0, 0, 255, 1)">var</span> imgold = document.getElementById("cat"<span style="color: rgba(0, 0, 0, 1)">);       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通过修改元素的属性,做的替换</span><span style="color: rgba(0, 128, 0, 1)">       //</span><span style="color: rgba(0, 128, 0, 1)"> img.setAttribute("src","../lagou-html/img/2.jpg");</span>       <span style="color: rgba(0, 0, 255, 1)">var</span> imgnew = document.createElement("img"<span style="color: rgba(0, 0, 0, 1)">);       imgnew.setAttribute(</span>"src","../lagou-html/img/1.jpg"<span style="color: rgba(0, 0, 0, 1)">);       imgold.parentNode.replaceChild( imgnew, imgold );     }   </span></script> </body></pre> <h3>4.3 事件</h3> <p>js捕获某个动作而做出的反馈</p> <p>HTML 事件的例子:</p> <ul> <li>当用户点击鼠标时</li> <li>当网页已加载时</li> <li>当图片已加载时</li> <li>当鼠标移动到元素上时</li> <li>当输入字段被改变时</li> <li>当 HTML 表单被提交时</li> <li>当用户触发按键时</li> </ul> <h4>4.3.1 窗口事件 (Window Events)</h4> <p>仅在 body 和 frameset 元素中有效。</p> <ul> <li>onload 当文档被载入时执行脚本</li> </ul> <pre><body onload="test()">   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> test() {       document.write(</span>"哈哈哈哈"<span style="color: rgba(0, 0, 0, 1)">);     }   </span></script> </body></pre> <h4>4.3.2 表单元素事件 (Form Element Events)</h4> <p>仅在表单元素中有效。</p> <ul> <li>onblur 当元素失去焦点时执行脚本</li> <li>onfocus 当元素获得焦点时执行脚本</li> </ul> <pre><body>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> a() {       console.log(</span>"获得焦点==被激活"<span style="color: rgba(0, 0, 0, 1)">);     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> b() {       console.log(</span>"失去焦点"<span style="color: rgba(0, 0, 0, 1)">);     }   </span></script>   <form action="">     <p>帐号:<input onfocus="a()" onblur="b()" /></p>     <p>密码:<input /></p>   </form> </body></pre> <h4>4.3.3 鼠标事件 (Mouse Events)</h4> <ul> <li>onclick 当鼠标被单击时执行脚本</li> <li>ondblclick 当鼠标被双击时执行脚本</li> <li>onmouseout 当鼠标指针移出某元素时执行脚本</li> <li>onmouseover 当鼠标指针悬停于某元素之上时执行脚本</li> </ul> <p></p> <pre><style><span style="color: rgba(0, 0, 0, 1)">   img{     width: </span>30%<span style="color: rgba(0, 0, 0, 1)">;     border: 5px solid white;   } </span></style> <body>   <img src="img/logo.png" onmouseover="shang(this)" onmouseout="xia(this)"<span style="color: rgba(0, 0, 0, 1)">   onclick</span>="dan()">   <img src="img/logo.png" onmouseover="shang(this)" onmouseout="xia(this)"<span style="color: rgba(0, 0, 0, 1)">   ondblclick</span>="shuang()">   <img src="img/logo.png" onmouseover="shang(this)" onmouseout="xia(this)" >   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> dan(){       alert(</span>"点了一下"<span style="color: rgba(0, 0, 0, 1)">);     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> shuang(){       alert(</span>"连续快读点两下"<span style="color: rgba(0, 0, 0, 1)">);     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> shang(img){       img.style.border </span>= "5px solid red"<span style="color: rgba(0, 0, 0, 1)">;     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> xia(img){       img.style.border </span>= "5px solid white"<span style="color: rgba(0, 0, 0, 1)">;     }   </span></script> </body></pre> <h4>4.3.4 键盘事件</h4> <ul> <li>onkeydown 按下去</li> <li>onkeyup 弹上来</li> </ul> <pre><script><span style="color: rgba(0, 0, 0, 1)">   window.onkeydown </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> event:事件源(按键)</span><span style="color: rgba(0, 128, 0, 1)">     //</span><span style="color: rgba(0, 128, 0, 1)"> console.log( "按键编码:"+event.keyCode );</span>     <span style="color: rgba(0, 0, 255, 1)">if</span>(event.keyCode == "13"){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 回车键</span>       alert("登录成功!"<span style="color: rgba(0, 0, 0, 1)">);     }   }   window.onkeyup </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){     console.log(event.keyCode); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 按住按键不松手是不会触发的。当松手时,按键回弹则触发</span><span style="color: rgba(0, 0, 0, 1)">   }   </span></script></pre> <h4>4.3.5 事件冒泡</h4> <ul> <li>创建两个div,一个大一些,一个小一些</li> </ul> <pre><style><span style="color: rgba(0, 0, 0, 1)">   #father {     width: 200px;     height: 200px;     background: black;     padding: 10px;   }   #child {     width: 100px;     height: 100px;     background: greenyellow;   } </span></style> <body>   <div id="father">     <div id="child"></div>   </div>   <script>     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 代码不重要,重要是知道这个事件发生,是正常现象</span>     document.getElementById("father").addEventListener("click", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {       alert(</span>"父级元素的事件被触发:" + <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.id);     });     document.getElementById(</span>"child").addEventListener("click", <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e) {       e.stopPropagation() </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取消事件的冒泡机制</span>       alert("子级元素的事件被触发:" + <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.id);     });   </span></script> </body></pre> <p>先子,后父。事件的触发顺序*自内向外*,这就是事件冒泡;</p> <h4>4.3.6 事件捕获</h4> <ul> <li>还是刚才创建两个div,一个大一些,一个小一些</li> </ul> <pre><style><span style="color: rgba(0, 0, 0, 1)">   #father {     width: 200px;     height: 200px;     background: black;     padding: 10px;   }   #child {     width: 100px;     height: 100px;     background: greenyellow;   } </span></style></pre> <p><body><br>  <div id="father"><br>    <div id="child"></div><br>  </div><br>  <script><br>    document.getElementById("father").addEventListener("click",function(){<br>      alert("父级:" + this.id);<br>    },true);<br>    document.getElementById("child").addEventListener("click",function(){<br>      alert("子级:" + this.id);<br>    },true);<br>  </script><br></body></p> <h3>4.4 面向对象OOP</h3> <ul> <li>使用Object创建通用对象</li> </ul> <pre><span style="color: rgba(0, 0, 255, 1)">var</span> user = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Object(); user.name </span>= "吕布"<span style="color: rgba(0, 0, 0, 1)">; user.age </span>= 21<span style="color: rgba(0, 0, 0, 1)">; user.say </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){   console.log(</span>"大家好,我叫:"+<span style="color: rgba(0, 0, 255, 1)">this</span>.name+",我今年"+<span style="color: rgba(0, 0, 255, 1)">this</span>.age+"岁了!"<span style="color: rgba(0, 0, 0, 1)">); } user.say(); </span><span style="color: rgba(0, 0, 255, 1)">var</span> dog = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Object(); dog.nickname </span>= "屎尿多"<span style="color: rgba(0, 0, 0, 1)">; dog.wang </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){   console.log(</span>"我饿了,我要拆家了!"<span style="color: rgba(0, 0, 0, 1)">); } dog.wang();</span></pre> <ul> <li>使用构造函数</li> </ul> <pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> userinfo(name , age){   </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;   </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;   </span><span style="color: rgba(0, 0, 255, 1)">this</span>.say = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){     console.log(</span>"大家好,我叫:"+<span style="color: rgba(0, 0, 255, 1)">this</span>.name+",我今年"+<span style="color: rgba(0, 0, 255, 1)">this</span>.age+"岁了!"<span style="color: rgba(0, 0, 0, 1)">);   }   } </span><span style="color: rgba(0, 0, 255, 1)">var</span> user = <span style="color: rgba(0, 0, 255, 1)">new</span> userinfo("詹姆斯",35<span style="color: rgba(0, 0, 0, 1)">); user.say();</span></pre> <ul> <li>使用直接量</li> </ul> <pre><span style="color: rgba(0, 0, 255, 1)">var</span> user =<span style="color: rgba(0, 0, 0, 1)"> {   username : </span>"孙悟空"<span style="color: rgba(0, 0, 0, 1)">,   age : </span>527<span style="color: rgba(0, 0, 0, 1)">,   say : </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(){     console.log(</span>"大家好,我叫:"+<span style="color: rgba(0, 0, 255, 1)">this</span>.username+",我今年"+<span style="color: rgba(0, 0, 255, 1)">this</span>.age+"岁了!"<span style="color: rgba(0, 0, 0, 1)">);   } }; user.say();</span></pre> <h3>4.5 JSON</h3> <ul> <li>大家在互联网上来回传递数据,如果没有一个统一的格式,解析起来的难度很大(每个人的编码喜好不一样)</li> <li>JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。</li> <li>易于人阅读和编写,同时也易于机器解析和生成 <ul> <li>{ <ul> <li>属性1:值1,</li> <li>属性2:值2,</li> <li>。。。。</li> </ul> </li> <li>}</li> </ul> </li> </ul> <pre><script>   <span style="color: rgba(0, 0, 255, 1)">var</span> json1 = { username: "吕布", age: 31<span style="color: rgba(0, 0, 0, 1)"> };   console.log(</span>"姓名:" + json1.username + ",年龄:" + json1.age + "岁"<span style="color: rgba(0, 0, 0, 1)">);   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> json数组</span>   <span style="color: rgba(0, 0, 255, 1)">var</span> josnarr = [{ name: "貂蝉", age: 18 }, { name: "小乔", age: 17<span style="color: rgba(0, 0, 0, 1)"> }];   console.log(</span>"貂蝉" + josnarr[0].age + "岁了"<span style="color: rgba(0, 0, 0, 1)">);   console.log(</span>"小乔" + josnarr[1].age + "岁了"<span style="color: rgba(0, 0, 0, 1)">);   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 复杂的json对象</span>   <span style="color: rgba(0, 0, 255, 1)">var</span> <span style="color: rgba(0, 0, 255, 1)">long</span> =<span style="color: rgba(0, 0, 0, 1)"> {     name: </span>"赵云"<span style="color: rgba(0, 0, 0, 1)">,     sex: </span>"男"<span style="color: rgba(0, 0, 0, 1)">,     hobby: [</span>"玉兰白龙驹", "龙胆亮银枪", "青釭剑"<span style="color: rgba(0, 0, 0, 1)">]   };   console.log(</span><span style="color: rgba(0, 0, 255, 1)">long</span>.name + "的主攻武器:" + <span style="color: rgba(0, 0, 255, 1)">long</span>.hobby[1<span style="color: rgba(0, 0, 0, 1)">]); </span></script></pre> <h2>5. BOM操作</h2> <p>就是javascript对浏览器的一些常规操作的方法</p> <h3>5.1 window对象</h3> <p></p> <pre><button onclick="kai()">极速入职</button> <script>   <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> kai(){     window.open(</span>"http://lagou.com" , "拉勾网"<span style="color: rgba(0, 0, 0, 1)"> ,     </span>"width=500,height=300,left=400"<span style="color: rgba(0, 0, 0, 1)">);     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> window.open("http://lagou.com" , "拉勾网" , "fullscreen=yes"); // IE才生效</span><span style="color: rgba(0, 0, 0, 1)">   } </span></script></pre> <h4>5.1.1 screen屏幕对象</h4> <p>我想知道我的电脑屏幕多大?实际上,得到的就是分辨率</p> <pre><body>   <button onclick="kai()">求大小</button> </body> <script>   <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> kai() {     alert(window.screen.width </span>+ "|" +<span style="color: rgba(0, 0, 0, 1)"> window.screen.height);   } </span></script></pre> <h4>5.1.2 location定位</h4> <p>包含有关当前 URL 的信息,通常用来做页面跳转</p> <pre><button onclick="test()">测试</button> <script>   <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> test(){     console.log( </span>"当前页面的URL路径地址:"+<span style="color: rgba(0, 0, 0, 1)"> location.href );     location.reload(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 重新加载当前页面(刷新)</span>     location.href = "http://lagou.com"; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 跳转页面</span>   <span style="color: rgba(0, 0, 0, 1)">} </span></script></pre> <h4>5.1.3 history浏览器历史</h4> <p>history对象会记录浏览器的痕迹</p> <ul> <li>a.html <ul> <li> <pre><a href="b.html">去b页面</a></pre> </li> </ul> </li> <li>b.html <ul> <li>   <pre><button onclick="hui()">返回</button> <script>   <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> hui(){     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">history.go(-1); //上一级页面</span>     history.back(); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 与go(-1)是等价的</span>   <span style="color: rgba(0, 0, 0, 1)">} </span></script></pre> </li> </ul> </li> </ul> <h4>5.1.4 navigator 导航(了解)</h4> <p>window.navigator 对象包含有关访问者浏览器的信息;</p> <pre><script>   <span style="color: rgba(0, 0, 255, 1)">var</span> str = ""<span style="color: rgba(0, 0, 0, 1)">;   str </span>+= "<p>浏览器的代号:"+ navigator.appCodeName +"</p>"<span style="color: rgba(0, 0, 0, 1)">;   str </span>+= "<p>浏览器的名称:"+ navigator.appName+"</p>"<span style="color: rgba(0, 0, 0, 1)">;   str </span>+= "<p>浏览器的版本:"+ navigator.appVersion+"</p>"<span style="color: rgba(0, 0, 0, 1)">;   str </span>+= "<p>硬件平台:"+ navigator.platform+"</p>"<span style="color: rgba(0, 0, 0, 1)">;   str </span>+= "<p>用户代理:"+ navigator.userAgent +"</p>"<span style="color: rgba(0, 0, 0, 1)">;   str </span>+= "<p>启用Cookies:"+navigator.cookieEnabled+"</p>"<span style="color: rgba(0, 0, 0, 1)">;   document.write(str); </span></script></pre> <h4>5.1.5 存储对象</h4> <p>用起来和我们在java中map很相似,都是键值对的方式存数据</p> <h5>5.1.5.1 本地存储 localStorage</h5> <p>在关闭窗口或标签页之后将会删除这些数据</p> <ul> <li>保存数据 <ul> <li> <pre>localStorage.setItem("name","curry");</pre> </li> </ul> </li> <li>提取数据 <ul> <li> <pre>localStorage.getItem("name");</pre> </li> </ul> </li> <li>删除数据 <ul> <li> <pre>localStorage.removeItem("name");</pre> </li> </ul> </li> </ul> <p>多样化操作</p> <pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 三种方式保存数据</span> localStorage["a"] = 1<span style="color: rgba(0, 0, 0, 1)">; localStorage.b </span>= 2<span style="color: rgba(0, 0, 0, 1)">; localStorage.setItem(</span>"c",3<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 查看数据类型</span> console.log( <span style="color: rgba(0, 0, 255, 1)">typeof</span> localStorage["a"<span style="color: rgba(0, 0, 0, 1)">] ) console.log( </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> localStorage["b"<span style="color: rgba(0, 0, 0, 1)">] ) console.log( </span><span style="color: rgba(0, 0, 255, 1)">typeof</span> localStorage["c"<span style="color: rgba(0, 0, 0, 1)">] ) </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第一种方式读取</span> <span style="color: rgba(0, 0, 255, 1)">var</span> a =<span style="color: rgba(0, 0, 0, 1)"> localStorage.a; console.log(a); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第二种方式读取</span> <span style="color: rgba(0, 0, 255, 1)">var</span> b = localStorage["b"<span style="color: rgba(0, 0, 0, 1)">]; console.log(b); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 第三种方式读取</span> <span style="color: rgba(0, 0, 255, 1)">var</span> c = localStorage.getItem("c"<span style="color: rgba(0, 0, 0, 1)">); console.log(c);</span></pre> <h5>5.1.5.2 会话存储 sessionStorage</h5> <ul> <li>会话,就是保持浏览器别关闭。</li> <li>关闭浏览就等于结束了一次会话。</li> <li>开启浏览器就意味着创建了一次会话。</li> <li>保存数据 <ul> <li> <pre>sessionStorage.setItem("name", "klay");</pre> </li> </ul> </li> <li>提取数据 <ul> <li> <pre><span style="color: rgba(0, 0, 255, 1)">var</span> lastname = sessionStorage.getItem("name");</pre> </li> </ul> </li> <li>删除指定键的数据 <ul> <li> <pre>sessionStorage.removeItem("name");</pre> </li> </ul> </li> <li>删除所有数据 <ul> <li> <pre>sessionStorage.clear();</pre> </li> </ul> </li> </ul> <p>案例:记录点击了几下按钮</p> <pre>  <button onclick="dian()">点我</button>   <h3 id="result"></h3>   <script>     <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> dian(){       </span><span style="color: rgba(0, 0, 255, 1)">if</span>( sessionStorage.getItem("clickCount"<span style="color: rgba(0, 0, 0, 1)">) ){         sessionStorage.setItem(</span>"clickCount"<span style="color: rgba(0, 0, 0, 1)">,         Number(sessionStorage.getItem(</span>"clickCount")) + 1<span style="color: rgba(0, 0, 0, 1)">);       }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{         sessionStorage.setItem(</span>"clickCount", 1<span style="color: rgba(0, 0, 0, 1)">);       }       document.getElementById(</span>"result").innerHTML = "已经点击了"+<span style="color: rgba(0, 0, 0, 1)">       sessionStorage.getItem(</span>"clickCount") +"次!"<span style="color: rgba(0, 0, 0, 1)">     }   </span></script></pre> <h3>5.2 计时操作</h3> <h4>5.2.1 周期性定时器 setInterval</h4> <p>setInterval(1,2):周期性触发代码exp (常用)</p> <p>1:执行语句</p> <p>2:时间周期,单位为毫秒</p> <ul> <li>案例:闪烁的字体 (1秒1变色)</li> </ul> <pre><body>   <h1 id="title">拉勾网:极速入职</h1>   <script>     <span style="color: rgba(0, 0, 255, 1)">var</span> colors = ["red","blue","yellow","pink","orange","black"<span style="color: rgba(0, 0, 0, 1)">];     </span><span style="color: rgba(0, 0, 255, 1)">var</span> i = 0<span style="color: rgba(0, 0, 0, 1)">;     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> bian(){       document.getElementById(</span>"title").style.color = colors[i++<span style="color: rgba(0, 0, 0, 1)">];       </span><span style="color: rgba(0, 0, 255, 1)">if</span>(i ==<span style="color: rgba(0, 0, 0, 1)"> colors.length){         i </span>= 0; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 颜色重新开始</span>       <span style="color: rgba(0, 0, 0, 1)">}     }     setInterval(bian,</span>100); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 每隔0.1秒,执行一次bian函数</span>   </script> </body></pre> <ul> <li>案例:在闪烁字体的基础上扩展,闪烁的电子时钟</li> </ul> <pre><body>   <h1 id="title"></h1>   <script>     <span style="color: rgba(0, 0, 255, 1)">var</span> colors = ["red","blue","yellow","pink","orange","black"<span style="color: rgba(0, 0, 0, 1)">];     </span><span style="color: rgba(0, 0, 255, 1)">var</span> i = 0<span style="color: rgba(0, 0, 0, 1)">;     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> bian(){       document.getElementById(</span>"title").style.color = colors[i++<span style="color: rgba(0, 0, 0, 1)">];       </span><span style="color: rgba(0, 0, 255, 1)">if</span>(i ==<span style="color: rgba(0, 0, 0, 1)"> colors.length){         i </span>= 0; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 颜色重新开始</span>       <span style="color: rgba(0, 0, 0, 1)">}     }     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> time(){ </span><span style="color: rgba(0, 0, 255, 1)">      var</span> d = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date();       </span><span style="color: rgba(0, 0, 255, 1)">var</span> str = d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"<span style="color: rgba(0, 0, 0, 1)">号       </span>"+d.getHours()+"点"+d.getMinutes()+"分"+d.getSeconds()+"秒"<span style="color: rgba(0, 0, 0, 1)">;       document.getElementById(</span>"title").innerHTML =<span style="color: rgba(0, 0, 0, 1)"> str;     }     setInterval(bian,</span>100); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 每隔1秒,执行一次变色函数bian</span>     setInterval(time,1000); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 每隔1秒,执行一次时间函数time</span>   </script> </body></pre> <h4>5.2.2 停止定时器 clearInterval</h4> <p>案例:模拟年会抽奖</p> <pre><body>   <img id="tu" src="../lagou-html/img/1.jpg" width="50%" />   <br />   <button onclick="begin()">开始</button>   <button onclick="stop()">停止</button>   <script>     <span style="color: rgba(0, 0, 255, 1)">var</span> arr = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"<span style="color: rgba(0, 0, 0, 1)">];     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> begin() {       timer </span>= setInterval(bian, 100); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 没有使用var,所以timer是全局变量</span>     <span style="color: rgba(0, 0, 0, 1)">}     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> stop() {       clearInterval(timer); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 停止定时器</span>     <span style="color: rgba(0, 0, 0, 1)">}     </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> bian() {       </span><span style="color: rgba(0, 0, 255, 1)">var</span> i = Math.floor(Math.random() * arr.length); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 0-4</span>       document.getElementById("tu").src = "../lagou-html/img/" +<span style="color: rgba(0, 0, 0, 1)"> arr[i];     }   </span></script> </body></pre> <h4>5.2.3 一次性定时器 setTimeout</h4> <p>相当于延迟的效果,只执行一次</p> <pre><script>   <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> bian(){     document.body.style.backgroundColor </span>= "red"<span style="color: rgba(0, 0, 0, 1)">;   }   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3秒之后调用</span>   setTimeout(bian,3000<span style="color: rgba(0, 0, 0, 1)">); </span></script></pre> </div> <!--conend--> <div class="p-2"></div> <div class="arcinfo my-3 fs-7 text-center"> <a href='/t/etagid32-0.html' class='tagbtn' target='_blank'>JavaScript</a> </div> <div class="p-2"></div> </div> <div class="p-2"></div> <!--xg--> <div class="lbox p-4 shadow-sm rounded-3"> <div class="boxtitle"><h2 class="fs-4">相关</h2></div> <hr> <div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-564695.html">javascript基础知识(29) 实现继承的方式</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-558708.html">7个有用的JavaScript技巧</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-558091.html">JavaScript跳出嵌套循环</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-556053.html">JavaScript常用对象介绍</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-555213.html">JavaScript学习day03</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-554796.html">推荐12个最好的 JavaScript 图形绘制库</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-554786.html">javascript模板字符串(反引号)</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-554315.html">javascript 新版本的语法(ECS6)</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-553059.html">javascript设计模式阅读后的感悟与总结</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-551895.html">【译】理解JavaScript中的柯里化</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-551134.html">javascript性能优化</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div><div class="row g-0 py-2 border-bottom align-items-center"> <div class="col-7 col-lg-11 border-lg-end"> <h3 class="fs-6 mb-0 mb-lg-2"><a href="/a/1-549759.html">图说js中的this——深入理解javascript中this指针</a></h3> <div class="ltag fs-8 d-none d-lg-block"> </div> </div> </div> <!----> <!----> </div> <!--xgend--> </div> <div class="col-lg-3 col-12 p-0 ps-lg-2"> <!--box--> <!--boxend--> <!--<div class="p-2"></div>--> <!--box--> <div class="lbox p-4 shadow-sm rounded-3"> <div class="boxtitle pb-2"><h2 class="fs-4"><a href="#">标签</a></h2></div> <div class="clearfix"></div> <ul class="m-0 p-0 fs-7 r-tag"> </ul> <div class="clearfix"></div> </div> <!--box end--> </div> </div> </div> </main> <div class="p-2"></div> <footer> <div class="container-fluid p-0 bg-black"> <div class="container p-0 fs-8"> <p class="text-center m-0 py-2 text-white-50">一品网 <a class="text-white-50" href="https://beian.miit.gov.cn/" target="_blank">冀ICP备14022925号-6</a></p> </div> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?6e3dd49b5f14d985cc4c6bdb9248f52b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </footer> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.min.js"></script> </body> </html>