JS红宝书·读书笔记

JS红宝书·读书笔记_第1张图片

JavaScript高级程序设计

花了半个多月的时间,终于又把“JS红宝书”又撸了一遍。

第一次读“JS红宝书”还是2015年初学JS的时候,那时候只是把语法部分读了一遍,还有一些浏览器相关知识做了下了解,大概也就读了半本的样子,
就开始了用JS进行开发了,在成长的道路上遇见了JQuery,当时真的是感觉到JQuery太友好了,慢慢放下了原生开发。

现在呢,更多的时候是在用框架进行开发,越来越觉得自己的JS基础很缺乏,然后就开启了“JS红宝书”二刷之路。

下面就把书中自己觉得重要的、没有掌握的知识整理出来。因为我觉得还是会三刷“JS红宝书”,希望把这本700多页的书越读越薄,勉励。

在HTML中使用JavaScript


async

加载外部脚本文件,通知浏览器立即下载,异步执行。

noscript元素

noscript标签显示条件:

  • 浏览器不支持脚本
  • 浏览器支持脚本,但是脚本被禁用

基本概念


语法

标识符

  • 第一个字符必须是一个字母、下划线或者一个美元符号
  • 其他字符可以是字母、下划线、美元或者数字

严格模式

支持严格模式的浏览器包括:IE10+、Firefox4+、Safari 5.1+、Opera 12+和Chrome。

数据类型

undefined

对未初始化的变量执行typeof操作会返回undefined值,而对于未声明的变量执行typeof操作同样会返回undefined值。

null

       
       
       
       
1
       
       
       
       
typeof null // -> object

undefined值派生自null值。

       
       
       
       
1
       
       
       
       
console.log(null == undefind) // -> true

isFinite()

测试一个数值是不是无穷值。

Number.NEGATIVE_INFINITY:负无穷
Number.POSITION_INFINITY:正无穷

NaN

在ECMAScript中,任何数值除以0会返回NaN

isNaN()接受一个参数,确定这个参数是否”不是数值”。

数值转换

Number()

  • 如果是null,返回0
  • 如果是undefined,返回NaN

parseInt()
在ES5 中不支持解析八进制的能力。

       
       
       
       
1
       
       
       
       
parseInt('070'); // -> 70 not 56

通过第二个参数,指定转换基数(进制)默认十进制。

字符串

  • ECMAScript中的字符串是不可变的
  • toString()

在调用数值的toString方法,可以传递一个参数:输出数值的基数。没有toString方法的则返回该值的字面量

       
       
       
       
1
2
       
       
       
       
var num = 10;
console.log(num.toString(2)); // -> '1010'

object类型

Object类型的属性方法:

  • constructor
  • hasOwnProperty(propertyName)
  • isPrototypeOf(obj)
  • propertyIsEnumerable(propertyName)
  • toLocalString()
  • toString()
  • valueOf()

操作符

  • ++ or --

前置与后置的区别

       
       
       
       
1
2
3
4
5
6
7
8
9
       
       
       
       
var num1 = 2;
var num2 = 20;
var num3 = --num1 + num2; // 21
var num4 = num1 + num2; // 21
var num5 = 2;
var num6 = 20;
var num7 = num5-- + num6; // 22
var num8 = num5 + num6; // 21

  • 一元加操作符用于强制类型转换,隐式Number()效果

for-in 语句

for-in语句是一种精确的迭代语句,可以用来枚举对象的属性。

通过for-in循环输出的属性名的顺序是不可预测的。

如果要迭代的对象的变量值为nullundefinedfor-in语句会抛出错误。ES5更正了这一行为,不再抛出错误,只是不再执行循环体。

建议:在是使用for-in循环之前,先检查对象值是不是null或者undefined

变量、作用域和内存问题


基本类型和引用类型

复制变量值

  • 复制基本类型值,这两个变量相互独立,互不影响。
  • 复制引用类型(对象),值引用是一个指针,改变其中一个对象,会影响另一个对象。

传递参数

       
       
       
       
1
2
3
4
5
6
       
       
       
       
function setName(obj) {
obj.name = "Nicholas";
}
var person = new Object();
setName(person);
alert(person.name); //"Nicholas"

以上代码中创建一个对象,并将其保存在了变量person 中。然后,这个变量被传递到setName()函数中之后就被复制给了obj。在这个函数部,obj 和person 引用的是同一个对象。换句话说,即使这个变量是按值传递的,obj 也会按引用来访问同一个对象。于是,当在函数内部为obj 添加name属性后,函数外部的person 也将有所反映;因为person 指向的对象在堆内存中只有一个,而且是全局对象。有很多开发人员错误地认为:在局部作用域中修改的对象会在全局作用域中反映出来,就说明参数是按引用传递的。为了证明对象是按值传递的,我们再看一看下面这个经过修改的例子:

       
       
       
       
1
2
3
4
5
6
7
8
       
       
       
       
function setName(obj) {
obj.name = "Nicholas";
obj = new Object();
obj.name = "Greg";
}
var person = new Object();
setName(person);
alert(person.name); //"Nicholas"

这个例子与前一个例子的唯一区别,就是在setName()函数中添加了两行代码:一行代码为obj重新定义了一个对象,另一行代码为该对象定义了一个带有不同值的name属性。在把person传递给setName()后,其name 属性被设置为”Nicholas”。然后,又将一个新对象赋给变量obj,同时将其name属性设置为”Greg”。如果person 是按引用传递的,那么person 就会自动被修改为指向其name属性值为”Greg”的新对象。但是,当接下来再访问person.name 时,显示的值仍然是”Nicholas”。这说明即使在函数内部修改了参数的值,但原始的引用仍然保持未变。实际上,当在函数内部重写obj时,这个变量引用的就是一个局部对象了。而这个局部对象会在函数执行完毕后立即被销毁。

检测类型

虽然在检测基本数据类型时typeof 是非常得力的助手,但在检测引用类型的值时,这个操作符的用处不大。通常,我们并不是想知道某个值是对象,而是想知道它是什么类型的对象。为此,ECMAScript提供了instanceof 操作符。

延长作用域

  • try-catch语句中的catch
  • with语句

小结

  • 基本类型值在内存中占据固定大小的空间,因此被保存在栈内存中;
  • 从一个变量向另一个变量复制基本类型的值,会创建这个值的一个副本;
  • 引用类型的值是对象,保存在堆内存中;
  • 包含引用类型值的变量实际上包含的并不是对象本身,而是一个指向该对象的指针;

引用类型


Array类型

检测数组

       
       
       
       
1
2
3
       
       
       
       
if (value instanceof Array) {
}

ECMAScript5新增了 Array.isArray()方法

       
       
       
       
1
2
3
       
       
       
       
if (Array.isArray(value)) {
}

sort方法

该方法有缺陷,sort()方法会调用每个数组项的toString()转型方法,然后比较字符串进行排序。

       
       
       
       
1
2
3
       
       
       
       
var values = [0, 1, 5, 10, 15];
values.sort();
alert(values); //0,1,10,15,5

因此sort()方法接受一个比较函数作为参数。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
       
       
       
       
function compare(value1, value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
}
var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); //0,1,5,10,15

splice方法

splice方法始终返回一个数组,该数组包含了从原始数组中删除的项。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
       
       
       
       
var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1); // 删除第一项
alert(colors); // green,blue
alert(removed); // red,返回的数组中只包含一项
removed = colors.splice(1, 0, "yellow", "orange"); // 从位置1 开始插入两项
alert(colors); // green,yellow,orange,blue
alert(removed); // 返回的是一个空数组
removed = colors.splice(1, 1, "red", "purple"); // 插入两项,删除一项
alert(colors); // green,red,purple,orange,blue
alert(removed); // yellow,返回的数组中只包含一项

迭代方法

ECMAScript5为数组定义了5个迭代方法。

  • every(): 对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true。
  • filter(): 对数组中的每一项运行给定函数,返回该函数会返回true 的项组成的数组。
  • forEach(): 对数组中的每一项运行给定函数。这个方法没有返回值。
  • map(): 对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。
  • some(): 对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true。

归并方法

ECMAScript 5 还新增了两个归并数组的方法。

  • reduce()
  • reduceRight()
    reduce()reduceRight()的函数接收4 个参数:前一个值、当前值、项的索引和数组对象。
             
             
             
             
    1
    2
    3
    4
    5
             
             
             
             
    var values = [1, 2, 3, 4, 5];
    var sum = values.reduce((prev, cur, index, array) => {
    return prev + cur;
    });
    console.log(sum);

RegExp类型

正则表达式中的元字符

( [ { \ ^ $ | ) ? * + . ] }

:匹配元字符必须转义

RegExp 构造函数

接受两个参数: 一个是要匹配的字符串模式,另一个是可选的标志字符串。

       
       
       
       
1
2
3
       
       
       
       
var pattern1 = /[bc]at/i;
// 等价于
var pattern2 = new RegExp('[bc]at', 'i');

:由于RegExp构造函数的模式是字符串,所以在某些情况下要对字符串进行双重转义,所有元字符都必须双重转义。例如\n在字符串中被转义为\\n,而在正则表达式字符串中就会变成\\\\n

RegExp实例方法

exec()方法

该方法是专门为捕获组而设计的。exec()接受一个参数,即要应用模式的字符串,然后返回包含第一个匹配项信息的数组;或者在没有匹配项的情况下返回null。返回的数组虽然是Array 的实例,但包含两个额外的属性:index 和input。其中,index 表示匹配项在字符串中的位置,而input 表示应用正则表达式的字符串。在数组中,第一项是与整个模式匹配的字符串,其他项是与模式中的捕获组匹配的字符串(如果模式中没有捕获组,则该数组只包含一项)。

       
       
       
       
1
2
3
4
5
6
7
8
9
       
       
       
       
var text = "mom and dad and baby";
var pattern = /mom( and dad( and baby)?)?/gi;
var matches = pattern.exec(text);
alert(matches.index); // 0
alert(matches.input); // "mom and dad and baby"
alert(matches[0]); // "mom and dad and baby"
alert(matches[1]); // " and dad and baby"
aler t(matches[2]); // " and baby"

对于exec()方法而言,即使在模式中设置了全局标志(g),它每次也只会返回一个匹配项。在不设置全局标志的情况下,在同一个字符串上多次调用exec()将始终返回第一个匹配项的信息。而在设置全局标志的情况下,每次调用exec()则都会在字符串中继续查找新匹配项。

test()方法

接受一个字符串参数。在模式与该参数匹配的情况下返回true;否则,返回false

       
       
       
       
1
2
3
4
5
6
       
       
       
       
var text = "000-00-0000";
var pattern = /\d{3}-\d{2}-\d{4}/;
if (pattern.test(text)){
alert("The pattern was matched.");
}

RegExp实例继承的toLocaleString()toString()方法都会返回正则表达式的字面量,与创建正则表达式的方式无关。

       
       
       
       
1
2
3
       
       
       
       
var pattern = new RegExp("\\[bc\\]at", "gi");
alert(pattern.toString()); // /\[bc\]at/gi
alert(pattern.toLocaleString()); // /\[bc\]at/gi

Function类型

函数声明与函数表达式

解析器会率先读取函数声明,并使其在执行任何代码之前可用;至于函数表达式,则必须等到解析器执行到它所在的代码行,才会真正被解释执行。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
       
       
       
       
// ok
alert(sum(10,10));
function sum(num1, num2){
return num1 + num2;
}
// unexpected identifier(意外标识符)
alert(sum(10,10));
var sum = function(num1, num2){
return num1 + num2;
};

:要访问函数的指针而不执行函数的话,必须去掉函数名后的那对圆括号。

函数内部属性

  • arguments
  • this

arguments具有一个callee属性,该属性是一个指针,指向拥有这个arguments对象的函数。

       
       
       
       
1
2
3
4
5
6
7
       
       
       
       
function factorial(num){
if (num <=1) {
return 1;
} else {
return num * factorial(num-1)
}
}

等价于

       
       
       
       
1
2
3
4
5
6
7
       
       
       
       
function factorial(num){
if (num <=1) {
return 1;
} else {
return num * arguments.callee(num-1)
}
}

达到一种解耦的效果。

ECMAScript 5也规范了一个函数对象属性:caller(看着很像callee),这个属性中保存着调用当前函数的函数的引用,如果实在全局作用域中调用当前函数,它的值为null

       
       
       
       
1
2
3
4
5
6
7
       
       
       
       
function outer(){
inner();
}
function inner(){
alert(inner.caller);
}
outer();

inner.caller指向outer()。为了实现更松散的耦合,也可以通过argument.callee.caller来访问相同的信息。

       
       
       
       
1
2
3
4
5
6
7
       
       
       
       
function outer() {
inner();
}
function inner() {
alert(arguments.callee.caller);
}
outer();

:当函数在严格模式下运行时,访问arguments.callee 会导致错误。ECMAScript 5 还定义了arguments.caller属性,但在严格模式下访问它也会导致错误,而在非严格模式下这个属性始终是undefined。定义这个属性是为了分清arguments.caller 和函数的caller 属性。以上变化都是为了加强这门语言的安全性,这样第三方代码就不能在相同的环境里窥视其他代码了。
严格模式还有一个限制:不能为函数的caller 属性赋值,否则会导致错误。

函数属性和方法

每个函数都包含两个属性:

  • length: 表示函数希望接收的命名参数的个数
  • prototype: 保存实例方法

每个函数都包含两个非继承而来的方法:

  • apply()
  • call()
    这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this 对象的值。首先,apply()方法接收两个参数:一个是在其中运行函数的作用域,另一个是参数数组。其中,第二个参数可以是Array 的实例,也可以是arguments对象。
    call()方法与apply()方法的作用相同,它们的区别仅在于接收参数的方式不同。对于call()方法而言,第一个参数是this 值没有变化,变化的是其余参数都直接传递给函数。换句话说,在使用call()方法时,传递给函数的参数必须逐个列举出来。

:在严格模式下,未指定环境对象而调用函数,则this 值不会转型为window。除非明确把函数添加到某个对象或者调用apply()call(),否则this 值将是undefined

在非严格模式下,callapply的第一个参数传递为nullundefined时,函数体内的this会指向默认的宿主对象,在浏览器中则是window

ECMAScript 5定义了一个方法bind(),这个方法会创建一个函数的实例,其this值会被绑定到传给bind()函数的值。

基本包装类型

使用new调用基本包装类型的构造函数,与直接调用同名的转型函数是不一样的。

       
       
       
       
1
2
3
4
5
6
       
       
       
       
var value = '25';
var number = Number(value); // 转型函数
console.log(typeof number); // 'number'
var obj = new Number(value); // 构造函数
console.log(typeof obj); // 'object'

Number类型

Number类型的toString()方法很特别,可以传递一个表示基数的参数。

       
       
       
       
1
2
3
4
5
6
       
       
       
       
var num = 10;
alert(num.toString()); //"10"
alert(num.toString(2)); //"1010"
alert(num.toString(8)); //"12"
alert(num.toString(10)); //"10"
alert(num.toString(16)); //"a"

String类型

字符方法
  • charAt()
  • charCodeAt()
       
       
       
       
1
2
3
4
       
       
       
       
var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"
alert(stringValue.charCodeAt(1)); //输出字符编码"101"
字符串操作方法
  • concat()
  • slice()
  • substr()
  • substring()

这些方法对原字符均没有任何影响。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
       
       
       
       
var stringValue = "hello ";
var result = stringValue.concat("world", "!");
alert(result); //"hello world!"
var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl"
// 参数是负值的情况下,它们的行为就不尽相同了。
// 其中,slice()方法会将传入的负值与字符串的长度相加,
// substr()方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0。
// 最后,substring()方法会把所有负值参数都转换为0。
alert(stringValue.slice(-3)); //"rld"
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.substr(-3)); //"rld"
alert(stringValue.slice(3, -4)); //"lo w"
alert(stringValue.substring(3, -4)); //"hel"
alert(stringValue.substr(3, -4)); //""(空字符串)
字符串位置方法
  • indexOf()
  • lastIndexOf()

两个方法的第二个参数,表示从字符串中哪个位置开始搜索。

trim()方法

ECMAScript 5方法

字符串转换大小写方法
  • toLowerCase()
  • toLocaleLowerCase()
  • toUpperCase()
  • toLocaleUpperCase()
字符串的模式匹配方法
  • match()
  • search()
  • replace()
  • split()

match()方法,在字符串上调用这个方法,本质上和调用RegExpexec()方法相同。match()方法只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。

       
       
       
       
1
2
3
4
5
6
7
8
       
       
       
       
var text = 'cat, bat, sat, fat';
var pattern = /.at/;
// 等价于 pattern.exec(text)
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0

search()方法的参数与match()方法相同,该方法返回字符串中第一个匹配项的索引,没有匹配项返回-1;个人认为serch()就是正则版的indexOf()

       
       
       
       
1
2
3
       
       
       
       
var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
aler t(pos); //1

ECMAScript提供了replace()方法,该方法接受两个参数,第一个参数可以是RegExp对象或者是一个字符串,第二个参数可以是一个字符串或者一个函数。

       
       
       
       
1
2
3
4
5
6
       
       
       
       
var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
alert(result); //"cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
aler t(result); //"cond, bond, sond, fond"

字符序列 替换文本
$$ $
$& 匹配整个模式的子字符串。RegExp.lastMatch
$' 匹配子字符串之前的字符串。RegExp.leftContext
$` 匹配的子字符串之后的字符串。 RegExp.rightContext
$n 匹配第n个捕获组的子字符串 n: 0~9
$nn 匹配第nn个捕获组的子字符串 nn: 01~99
       
       
       
       
1
2
3
       
       
       
       
var text = "cat, bat, sat, fat";
result = text.replace(/(.at)/g, "word ($1)");
alert(result); //word (cat), word (bat), word (sat), word (fat)

split()方法可以基于指定的分隔符(字符串 or RegExp对象)将一个字符串分割成多个子字符串,并将结构放在一个数组中。可以接受可选的第二个参数,用于指定数组的大小。

       
       
       
       
1
2
3
4
       
       
       
       
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2); //["red", "blue"]
var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]

localeCompare()方法

比较两个字符串,并返回下列值中的 一个:

  • 如果字符串在字母表中应该排在字符串参数之前,则返回一个负数
  • 如果字符串等于字符串参数,则返回0;
  • 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数
       
       
       
       
1
2
3
4
       
       
       
       
var stringValue = "yellow";
alert(stringValue.localeCompare("brick")); //1
alert(stringValue.localeCompare("yellow")); //0
alert(stringValue.localeCompare("zoo")); //-1
fromCharCode()方法

这个方法的任务是接收一个或多个字符编码,然后将它们转换成一个字符串。相当于charCodeAt()反操作。

       
       
       
       
1
       
       
       
       
alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"

Math 对象

  • min()
  • max()
  • ceil()
  • floor()
  • round()
  • random()

面向对象的程序设计

理解对象

属性类型

数据类型
  • [[Configurable]]: 表示能否通过delete删除属性从而重新定义属性,能够修改属性的特性,或者能否把属性修改为访问器属性
  • [[Enumerable]]: 表示能否通过for-in循环返回属性
  • [[Writable]]: 表示能否修改属性的值
  • [[Value]]: 包含这个属性的数据值。读取属性值的时候,从这个位置读;写入属性值的时候,把新值保存在这个位置。默认值undefined

要修改属性默认的特性,必须使用ECMAScript 5Object.defineProperty()方法。这个方法接受三个参数:属性所在对象,属性名和一个描述符对象。其中描述符对象的属性值必须是:configurableenumerablewritablevalue。设置其中一个或多个。

       
       
       
       
1
2
3
4
5
       
       
       
       
var person = {};
Object.defineProperty(person, 'name', {
writable: false,
value: 'Yeaseon'
});

Object.defineProperty()方法不能对configurable: false的对象进行修改。

访问器属性
  • [[Configurable]]: 表示能否通过delete删除属性从而重新定义属性,能否修改属性的特性,或者能否把属性修改为数据属性。
  • [[Enumerable]]: 表示能否通过for-in循环返回属性。
  • [[Get]]: 在读取属性时调用的函数,默认undefined
  • [[Set]]: 在写入属性时调用的函数,默认undefined
       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
       
       
       
       
var book = {
_year: 2004,
edition: 1
};
Object.defineProperty(book, "year", {
get: function(){
return this._year;
},
set: function(newValue){
if (newValue > 2004) {
this._year = newValue;
this.edition += newValue - 2004;
}
}
});
book.year = 2005;
alert(book.edition); //2

读取属性的特性

ECMAScript 5Object.getOwnPropertyDescriptor()方法,可以取得给定属性的描述符。该方法接收两个参数:属性所在的对象和要读取器描述符的属性名称,返回值是对象。

创建对象

工厂模式

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
       
       
       
       
function createPerson(name, age, job){
var o = new Object();
o.name = name;
o.age = age;
o.job = job;
o.sayName = function(){
alert(this.name);
};
return o;
}
var person1 = createPerson("Nicholas", 29, "Software Engineer");
var person2 = createPerson("Greg", 27, "Doctor");

构造函数模式

       
       
       
       
1
2
3
4
5
6
7
8
9
10
       
       
       
       
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert(this.name);
};
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");

原型模式

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
       
       
       
       
function Person(){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function(){
alert(this.name);
};
var person1 = new Person();
person1.sayName(); //"Nicholas"

Person.prototype.constructor会指向Personperson1并没有直接连接到构造函数Person

JS红宝书·读书笔记_第2张图片
  • 可以通过isPrototypeOf()方法来确定对象之间是否存在原型关系。从本质上讲,[[Prototype]]指向调用isPrototypeOf()方法的对象Person.prototype,则会返回true
       
       
       
       
1
       
       
       
       
alert(Person.prototype.isPrototypeOf(person1)); //true

ECMAScript 5增加了Object.getPrototypeOf()方法,该方法返回[[Prototype]]的值。

       
       
       
       
1
2
       
       
       
       
alert(Object.getPrototypeOf(person1) == Person.prototype); //true
alert(Object.getPrototypeOf(person1).name); //"Nicholas"

: 虽然可以通过对象实例person1访问保存在原型中的值,但却不能重写原型中的值。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       
       
       
       
function Person(){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function(){
alert(this.name);
};
var person1 = new Person();
var person2 = new Person();
person1.name = "Greg"; //实质是在实例上增加一个name属性
alert(person1.name); //"Greg"——来自实例
alert(person2.name); //"Nicholas"——来自原型

可以通过delete删除实例属性,从而继续访问原型中的属性。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       
       
       
       
function Person(){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function(){
alert(this.name);
};
var person1 = new Person();
person1.name = "Greg";
alert(person1.name); //"Greg"——来自实例
delete person1.name;
alert(person1.name); //"Nicholas"——来自原型

  • hasOwnProperty()方法可以检测一个属性是不是存在于实例,是则返回true
JS红宝书·读书笔记_第3张图片
  • in操作符
    (prop in obj)通过in操作符可以判定对象是否有该属性,不论是本身含有还是原型含有,都返回true
    可以通过in配合hasOwnProperty()确定该属性是存在于对象中还是原型中:

             
             
             
             
    1
    2
    3
    4
    5
    6
    7
             
             
             
             
    function detectProperty(obj, name) {
    if (name in obj) {
    obj.hasOwnProperty(name) ? '在对象中' : '在原型中';
    } else {
    console.log('不含有该属性');
    }
    }
  • ECMAScript 5Object.keys()方法可以取得对象上所有可枚举的实例属性。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
       
       
       
       
function Person(){
}
Person.prototype.name = "Nicholas";
Person.prototype.age = 29;
Person.prototype.job = "Software Engineer";
Person.prototype.sayName = function(){
alert(this.name);
};
var keys = Object.keys(Person.prototype);
alert(keys); //"name,age,job,sayName"
var p1 = new Person();
p1.name = "Rob";
p1.age = 31;
var p1keys = Object.keys(p1);
alert(p1keys); //"name,age"
  • Object.getOwnPropertyNames会得到所有实例属性,不论是否可枚举。
       
       
       
       
1
2
       
       
       
       
var keys = Object.getOwnPropertyNames(Person.prototype);
alert(keys); //"constructor,name,age,job,sayName"

简化Person.prototype写法:

       
       
       
       
1
2
3
4
5
6
7
8
9
10
       
       
       
       
function Person(){
}
Person.prototype = {
name : "Nicholas",
age : 29,
job: "Software Engineer",
sayName : function () {
alert(this.name);
}
};

这样写有一个缺陷,constructor属性则会等于Object,我们需要手动设置constructor

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
       
       
       
       
function Person(){
}
Person.prototype = {
constructor : Person,
name : "Nicholas",
age : 29,
job: "Software Engineer",
sayName : function () {
alert(this.name);
}
};

但这同时也会导致constructor[[Enumerable]]特性变成了true,默认情况下是false。再修改下写法:

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       
       
       
       
function Person(){
}
Person.prototype = {
name : "Nicholas",
age : 29,
job : "Software Engineer",
sayName : function () {
alert(this.name);
}
};
Object.defineProperty(Person.prototype, "constructor", {
enumerable: false,
value: Person
});

原型重写会导致构造函数与最初原型之间的联系切断。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
       
       
       
       
function Person(){
}
var friend = new Person();
Person.prototype = { //重写
constructor: Person,
name : "Nicholas",
age : 29,
job : "Software Engineer",
sayName : function () {
alert(this.name);
}
};
friend.sayName(); //error

JS红宝书·读书笔记_第4张图片

结合使用构造函数和原型模式

用构造函数模式定义实例属性,用原型模式定义方法和共享属性。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
       
       
       
       
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.friends = ["Shelby", "Court"];
}
Person.prototype = {
constructor : Person,
sayName : function(){
alert(this.name);
}
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
person1.friends.push("Van");
alert(person1.friends); //"Shelby,Count,Van"
alert(person2.friends); //"Shelby,Count"
alert(person1.friends === person2.friends); //false
alert(person1.sayName === person2.sayName); //true

继承

构造函数、原型和实例的关系:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型的内部指针。

原型链

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
       
       
       
       
function SuperType () {
this.property = true;
}
SuperType.prototype.getSuperValue = function () {
return this.property;
};
function SubType () {
this.subproperty = false;
}
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function () {
return this.subproperty;
};
var instance = new SubType();
console.log(instance.getSuperValue()); // true
JS红宝书·读书笔记_第5张图片
instanceof操作符

用来确定原型和实例之间的关系。

       
       
       
       
1
2
3
       
       
       
       
alert(instance instanceof Object); //true
alert(instance instanceof SuperType); //true
alert(instance instanceof SubType); //true

第二种方式就是isPrototypeOf()方法,只要原型链中出现过的原型,都可以说是该原型链所派生的实例的原型。

       
       
       
       
1
2
3
       
       
       
       
alert(Object.prototype.isPrototypeOf(instance)); //true
alert(SuperType.prototype.isPrototypeOf(instance)); //true
alert(SubType.prototype.isPrototypeOf(instance)); //true

函数表达式

由于有声明提升的存在,定义函数不要放在条件表达式中

       
       
       
       
1
2
3
4
5
6
7
8
9
       
       
       
       
if (condition) {
function sayHi () {
console.log('Hi');
}
} else {
function sayHi () {
console.log('Yo');
}
}

ECMAScript中属于无效语法,在不同浏览器中修正的做法并不一致。推荐的写法,如下:

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
       
       
       
       
var sayHi;
if (condition) {
sayHi = function () {
console.log('Hi');
}
} else {
sayHi = function () {
console.log('Yo');
}
}

这种函数表达式不存在声明提升,所以OK。

递归

函数作用域链

当某个函数被调用时,会创建一个执行环境及相应的作用域链。然后,使用arguments和其他命名参数的值来初始化函数的活动对象。在作用域链中,外部函数的活动对象始终处于第二位,外部函数的外部函数的活动对象处于第三位…,直到作用域终点的全局执行环境。

闭包

       
       
       
       
1
2
3
4
5
6
7
8
9
       
       
       
       
function createFunctions () {
var result = new Array();
for (var i=0; i < 10; i++){
result[i] = function(){
return i;
};
}
return result;
}
       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
       
       
       
       
function createFunction () {
var result = new Array();
for (var i = 0; i < 10; i++) {
result[i] = function (num) {
return function () {
return num;
};
}(i);
}
return result;
}

:在闭包中使用this对象可能会导致一些问题。匿名函数的执行环境具有全局性,因此其this对象通常指向window

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
       
       
       
       
var name = 'The window';
var obj = {
name: 'my object',
getNameFunc: function () {
return function () {
return this.nam;
}
}
}
console.log(obj.getNameFunc()()); // The Window (非严格模式)

模仿块级作用域

块级作用域

       
       
       
       
1
2
3
       
       
       
       
(function () {
// 这里是块级作用域
})();

BOM


window 对象

全局作用域

抛开全局变量会成为window对象的属性不谈,定义全局变量与在window对象上直接定义属性还是有一点差别:全局变量不能通过delete操作符删除,而直接定义在window对象上的定义的属性可以

窗口位置

获得窗口左边和上边的位置。

       
       
       
       
1
2
       
       
       
       
var leftPos = (typeof window.screenLeft == 'number') ? window.screenLeft : window.screenX;
var topPos = (typeof window.screenTop == 'number') ? window.screenTop : window.screenY;

Firefox支持screenXscreenY,其他浏览器均支持screenLeftscreenTop

但是还是需要注意一个问题:在IE Opera中,screenLeft screenTop 保存的的是可见区域的距离,也就是我们浏览器中不包含工具栏的区域与屏幕的距离;在ChromeFirefoxSafariscreenYscreenTop返回的是整个浏览器窗口相对于屏幕坐标的值。

窗口大小

IE9+、Firefox、Safari、Opera和Chrome均提供了4个属性innerWidthinnerHeightouterWidthouterHeight

  • IE9+、Safari和Firefox中,outerWidthouterHeight返回浏览器窗口本身的尺寸,而innerWidthinnerHeight则表示该容器中页面视图区的大小(减去边框宽度)
  • Chrome中,inner*outer*返回相同的值,即视口大小而非浏览器窗口的大小。
  • 在IE、Firefox、Safari、Opera和Chrome中,都能通过document.documentElement.clientWidthdocument.documentElement.clientHeight中保存了页面视口信息。

获取页面视口大小

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
       
       
       
       
var pageWidth = window.innerWidth,
pageHeight = window.innerHeight;
if (typeof pageWidth != 'number') {
if (document.compatMode == 'CSS1Compat') { // 浏览器标准模式
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
} else { // IE6 混杂模式
pageWidth = document.body.clientWidth;
pageHeight = document.doby.clientHeight;
}
}

  • resizeTo()接受浏览器窗口的新宽度和新高度
  • resizeBy()接受新窗口与原窗口的宽度和高度差。

这两个方法可能被浏览器禁用。

导航

如果是浏览器内置的屏蔽程序组织的弹出窗口,那么window.open()很可能会返回null

       
       
       
       
1
2
3
4
       
       
       
       
var newWindow = window.open('https://www.google.com.hk', '_blank');
if (newWindow == null) {
console.log('The popup was blocked!');
}

如果是浏览器扩展或其他程序组织的弹出窗口,那么window.open()通常会抛出一个错误。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
       
       
       
       
var blocked = false;
try {
var newWindow = window.open('https://www.google.com.hk', '_blank');
if (newWindow == null) {
blocked = true;
}
} catch (ex) {
blocked = true;
}
if (blocked) {
console.log('The popup was blocked');
}

location 对象

location对象的属性

  • hash
  • host
  • hostname:与host不同的是,不带端口号
  • href
  • pathname: 返回URL中的目录和(或)文件名
  • port
  • protocol
  • search:返回URL的查询字符串,这个字符串?开头

location对象的属性

  • appCodeName: 浏览器的名称,通常都是Mozilla
  • appMinorVersion:此版本信息
  • appName: 完整的浏览器名称
  • appVersion:浏览器的版本
  • buildID:浏览器编译版本
  • cookieEnabled:表示cookie是否可用
  • cpuClass:客户端计算机中使用的CPU类型
  • javaEnabled():表示当前浏览器中是否启用了java
  • language: 浏览器的主语言
  • mimeTypes:浏览器中注册的MIME类型数组
  • onLine:表示浏览器是都连接到因特网
  • oscpu:客户端计算机的操作系统或使用的CPU
  • platform:浏览器所在的系统平台
  • plugins:浏览器中安装的插件信息的数组
  • preference():设置用户的首选项
  • systemLanguage:操作系统的语言
  • userAgent:浏览器的用户代理字符串

DOM


节点层次

Node类型

每个节点都有一个nodeType属性,用于表明节点的类型。

  • Node.ELEMENT_NODE(1)
  • Node.ATTRIBUTE_NODE(2)
  • Node.TEXT_NODE(3)
  • Node.CDATA_SECTION_NODE(4)
  • Node.ENTITY_REFERENCE_NODE(5)
  • Node.ENTITY_NODE(6)
  • Node.PROCESSING_INSTRUCTION_NODE(7)
  • Node.COMMENT_NODE(8)
  • Node.DOCUMENT_NODE(9)
  • Node.DOCUMENT_TYPE_NODE(10)
  • Node.DOCUMENT_FRAGMENT_NODE(11)
  • Node.NOTATION_NODE(12)

为了确保跨浏览器兼容,将nodeType属性与数字值进行比较:

       
       
       
       
1
2
3
       
       
       
       
if (someNode.nodeType == 1) {
console.log('Node is an element');
}

  • nodeName属性
             
             
             
             
    1
    2
    3
             
             
             
             
    if (someNode.nodeType == 1) {
    var value = someNode.nodeName; // nodeName的值是元素的标签名
    }
节点关系
  • childNodes属性

每个节点都有一个childNodes属性,其中保存着一个NodeList对象,该对象是一种类数组对象。

  • parentNode属性

每个节点都有一个parentNode属性,该属性指向文档树中的父节点。包含在childNodes列表中的每个节点相互都是兄弟节点。使用previousSiblingnextSibling属性,可以访问其他兄弟节点。

:列表中第一个节点的previousSibling属性值为null,同理列表中最后一个节点的nextSibling属性也是null。父节点的firstChildlastChild属性分别指向其childNodes列表中的第一个和最后一个节点。如果不存在则为null

hasChildNodes()方法在节点包含一个或多个子节点的情况下返回true,比查询childNodes.length更简便。

最后一个属性ownerDocument,该属性指向表示整个文档的文档节点(root),直接返回根节点不需要一层层向上回溯。

操作节点
  • appendChild()

用于向childNodes列表的末尾添加一个节点。

       
       
       
       
1
2
3
       
       
       
       
var returnedNode = someNode.appendChild(newNode);
alert(returnedNode == newNode); //true
alert(someNode.lastChild == newNode); //true

任何DOM节点不可能同时出现在多个位置。

       
       
       
       
1
2
3
4
       
       
       
       
//someNode 有多个子节点
var returnedNode = someNode.appendChild(someNode.firstChild);
alert(returnedNode == someNode.firstChild); //false
alert(returnedNode == someNode.lastChild); //true

  • insertBefore()

把节点放在指定位置,该方法接受两个参数:要插入的节点和作为参考的节点。插入节点后,被插入的节点会变成参照节点的前一个兄弟节点。参照节点是null的话,insertBeforeappendChild执行相同的操作,都插入列表末尾。

       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
       
       
       
       
//插入后成为最后一个子节点
returnedNode = someNode.insertBefore(newNode, null);
alert(newNode == someNode.lastChild); //true
//插入后成为第一个子节点
var returnedNode = someNode.insertBefore(newNode, someNode.firstChild);
alert(returnedNode == newNode); //true
alert(newNode == someNode.firstChild); //true
//插入到最后一个子节点前面
returnedNode = someNode.insertBefore(newNode, someNode.lastChild);
alert(newNode == someNode.childNodes[someNode.childNodes.length-2]); //true

  • replaceChild()

替换节点,接受两个参数:要插入的节点和要替换的节点。

       
       
       
       
1
2
3
4
5
       
       
       
       
//替换第一个子节点
var returnedNode = someNode.replaceChild(newNode, someNode.firstChild);
//替换最后一个子节点
returnedNode = someNode.replaceChild(newNode, someNode.lastChild);

  • removeChild()

移除节点,接受一个参数:要被移除的节点。

       
       
       
       
1
2
3
4
5
       
       
       
       
//移除第一个子节点
var formerFirstChild = someNode.removeChild(someNode.firstChild);
//移除最后一个子节点
var formerLastChild = someNode.removeChild(someNode.lastChild);

  • cloneNode()

复制节点,接受一个布尔值,表示是否深复制。复制后返回的节点没有父节点,可以通过插入等操作手动指定。

       
       
       
       
1
2
3
4
5
       
       
       
       
var deepList = myList.cloneNode(true);
alert(deepList.childNodes.length); //3(IE < 9)或7(其他浏览器)
var shallowList = myList.cloneNode(false);
alert(shallowList.childNodes.length); //0

cloneNode方法不会复制DOM节点的js属性。IE存在一个bug,它会复制事件处理程序。

  • normalize()

稍后讨论

以上方法的返回值,都是被操作的节点。

Document类型

Document节点具有下列特征:

  • nodeType的值为9
  • nodeName的值为#document
  • nodeValue的值为null
  • parentNode的值为null
  • ownerDocument的值为null
  • 其子节点可能是一个DocumentType(最多一个)、Element(最多一个)、ProcessingInstructionComment
       
       
       
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
       
       
       
       
// 通过`documentElement`属性访问元素
var html = document.documentElement;
// 访问 元素
var body = document.body;
//
var doctype = document.doctype;
// </div> <div class="line" style="height:20px"> var title = document.title; </div> <div class="line" style="height:20px"> // 完整 url </div> <div class="line" style="height:20px"> var url = document.URL; </div> <div class="line" style="height:20px"> // domain 域名 </div> <div class="line" style="height:20px"> var domain = document.domain; </div> <div class="line" style="height:20px"> // 取得来源页面的URL(也就是导航到这页的页面) </div> <div class="line" style="height:20px"> var referrer = document.referrer; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 查找元素的方法:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.getElementById()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.getElementsByTagName()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.getElementsByName()</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 文档写入:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.write()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.writeln()</code>在字符串尾加换行符(\n)</li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <script type="text/javascript"> </div> <div class="line" style="height:20px"> document.write("<script type=\"text/javascript\" src=\"file.js\">" + "<\/script>"); </div> <div class="line" style="height:20px"> </script> </div></pre> </td> </tr> </tbody> </table> <h4 id="Element类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> Element类型</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code>类型提供了对元素标签名、子节点及特性的访问。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeType</code>的值为1</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeName</code>的值为元素的标签名</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeValue</code>的值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">null</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">parentNode</code>可能是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Document</code>或<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code></li> <li style="list-style:circle">其子节点可能是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Text</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Comment</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ProcessingInstruction</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">CDATASection</code>或<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">EntityReference</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 访问元素的标签名,可以使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeName</code>属性,也可以使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">tagName</code>属性,后者更直观。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <div id="myDiv"></div> </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var div = document.getElementById("myDiv"); </div> <div class="line" style="height:20px"> alert(div.tagName); //"DIV" </div> <div class="line" style="height:20px"> alert(div.tagName == div.nodeName); //true </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 操作特性的方法:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getAttribute()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setAttribute()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">removeAttribute()</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">attributes</code>属性</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code>类型是使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">attributes</code>属性的唯一一个DOM节点属性。<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">attributes</code>属性包含一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NamedNodeMap</code>。元素的每一个特性都由一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Attr</code>节点表示,每个节点都保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NamedNodeMap</code>对象中。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NamedNodeMap</code>对象的方法:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getNamedItem(name)</code>:返回<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeName</code>属性等于<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">name</code>的节点</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">removeNamedItem(name)</code>:从列表中移除<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeName</code>属性等于<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">name</code>的节点</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setNamedItem(node)</code>:向列表中添加节点,以节点的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeName</code>属性为索引</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">item(pos)</code>:返回位于数字<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">pos</code>位置处的节点</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">attributes</code>属性中包含一系列节点,每个节点的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeName</code>就是特性的名称,而节点<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeValue</code>就是特性的值。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var id = element.attributes.getNamedItem('id').nodeValue; </div> <div class="line" style="height:20px"> // 简写 </div> <div class="line" style="height:20px"> var id = element.attributes['id'].nodeValue; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 创建元素<br> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.createElement()</code>方法可以创建新元素,这个方法接受一个参数(标签名)<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var div = document.createElement('div'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="Text类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> Text类型</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 文本节点由<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Text</code>类型表示,包含的是可以照字面解释的纯文本内容。纯文本中可以包含转义后的HTML字符,但不能包含HTML代码。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeType</code>的值为3</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeName</code>的值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">#text</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nodeValue</code>的值为节点所包含的文本</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">parentNode</code>是一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 操作节点中的文本:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">appendData(text)</code>:将<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">text</code>添加到节点的末尾</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">deleteData(offset, count)</code>:从<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offset</code>指定的位置开始删除<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">count</code>个字符</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">insertData(offset, text)</code>:在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offset</code>指定的位置插入<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">text</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">replaceData(offset, count, text)</code>:用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">text</code>替换从<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offset</code>指定的位置开始到<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offset+count</code>为止的文本</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">splitText(offset)</code>:从<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offset</code>指定的位置将当前文本分成两个文本节点</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">substringData(offset, count)</code>:提取从<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offset</code>指定的位置开始到<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offset+count</code>为止处的字符串。</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在向DOM文档中插入文本之前,应该先对其进行<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">HTML</code>编码</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 创建文本节点</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.createTextNode()</code></li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var textNode = document.createTextNode("<strong>Hello</strong> world!"); </div></pre> </td> </tr> </tbody> </table> <h3 id="DOM-操作技术" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> DOM 操作技术</h3> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 使用函数实现加载外部<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JS</code>文件<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function loadScript(url) { </div> <div class="line" style="height:20px"> var script = document.createElement('script'); </div> <div class="line" style="height:20px"> script.type = 'text/javascript'; </div> <div class="line" style="height:20px"> script.src = url; </div> <div class="line" style="height:20px"> document.body.appendChild(script); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> loadScirpt('xx.js'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> IE将<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><script></code>视为一个特殊的元素,不允许DOM访问其子节点。不过可以使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><script></code>元素的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">text</code>属性指定<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JS</code>代码。</p> <h4 id="操作表格" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 操作表格</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div> <div class="line" style="height:20px"> 23 </div> <div class="line" style="height:20px"> 24 </div> <div class="line" style="height:20px"> 25 </div> <div class="line" style="height:20px"> 26 </div> <div class="line" style="height:20px"> 27 </div> <div class="line" style="height:20px"> 28 </div> <div class="line" style="height:20px"> 29 </div> <div class="line" style="height:20px"> 30 </div> <div class="line" style="height:20px"> 31 </div> <div class="line" style="height:20px"> 32 </div> <div class="line" style="height:20px"> 33 </div> <div class="line" style="height:20px"> 34 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> // create table </div> <div class="line" style="height:20px"> var table = document.createElement('table'); </div> <div class="line" style="height:20px"> table.border = 1; </div> <div class="line" style="height:20px"> table.width = '100%'; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> // create tbody </div> <div class="line" style="height:20px"> var tbody = document.createElement('tbody'); </div> <div class="line" style="height:20px"> table.appendChild(tbody); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> // create row1 </div> <div class="line" style="height:20px"> var row1 = document.createElement('tr'); </div> <div class="line" style="height:20px"> tbody.appendChild(row1); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var cell1_1 = document.createElement('td'); </div> <div class="line" style="height:20px"> cell1_1.appendChild(document.createTextNode('Cell 1,1')); </div> <div class="line" style="height:20px"> row1.appendChild(cell1_1); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var cell2_1 = document.createElement('td'); </div> <div class="line" style="height:20px"> cell2_1.appendChild(document.createTextNode('Cell 2,1')); </div> <div class="line" style="height:20px"> row1.appendChild(cell2_1); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> // create row2 </div> <div class="line" style="height:20px"> var row2 = document.createElement('tr'); </div> <div class="line" style="height:20px"> tbody.appendChild(row2); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var cell1_2 = document.createElement('td'); </div> <div class="line" style="height:20px"> cell1_2.appendChild(document.createTextNode('Cell 1,2')); </div> <div class="line" style="height:20px"> row1.appendChild(cell1_2); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var cell2_2 = document.createElement('td'); </div> <div class="line" style="height:20px"> cell2_2.appendChild(document.createTextNode('Cell 2,2')); </div> <div class="line" style="height:20px"> row1.appendChild(cell2_2); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> document.body.appendChild(table); </div></pre> </td> </tr> </tbody> </table> <h2 id="DOM-扩展" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> DOM 扩展</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="选择符-API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 选择符 API</h3> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">querySelector()</code>方法</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">querySelector()</code>方法接受一个<strong>CSS</strong>选择符,返回与该模式匹配的<strong>第一个元素</strong>,若没有,返回<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">null</code>。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 可以通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Document</code>类型调用,也可以通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code>类型调用,后者只会在该元素后代元素的范围内查找匹配的元素。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">querySelectorAll()</code>方法</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">querySelectorAll()</code>方法返回的是所有匹配的元素,是一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeList</code>实例。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">matchesSelector()</code>方法</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code>类型新增的一个方法,接受一个参数<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">CSS</code>选择符,如果调用元素与该选择符匹配,返回<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code>,否则返回<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">false</code>。</p> <h3 id="元素遍历" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 元素遍历</h3> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">childElementCount</code>:返回子元素(不包含文本节点和注释)的个数</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">firstElementChild</code>:指向第一个子元素</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">lastElementChild</code>:指向最后一个子元素</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">previousElementSibling</code>:指向前一个兄弟元素</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">nextElementSibling</code>:指向后一个兄弟元素</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 不同于前面的返回<strong>节点</strong>的方法。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> // 节点版本 </div> <div class="line" style="height:20px"> var i, </div> <div class="line" style="height:20px"> len, </div> <div class="line" style="height:20px"> child = element.firstChild; </div> <div class="line" style="height:20px"> while(child != element.lastChild){ </div> <div class="line" style="height:20px"> if (child.nodeType == 1){ //检查是不是元素 </div> <div class="line" style="height:20px"> processChild(child); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> child = child.nextSibling; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> // 元素版本 </div> <div class="line" style="height:20px"> var i, </div> <div class="line" style="height:20px"> len, </div> <div class="line" style="height:20px"> child = element.firstElementChild; </div> <div class="line" style="height:20px"> while(child != element.lastElementChild){ </div> <div class="line" style="height:20px"> processChild(child); //已知其是元素 </div> <div class="line" style="height:20px"> child = child.nextElementSibling; </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h3 id="HTML5" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> HTML5</h3> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getElementsByClassName()</code>方法</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">classList</code>属性,这个属性是新集合类型<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">DOMTokenList</code>的实例。 <ul style="list-style:none"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">add(value)</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">contains(value)</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">remove(value)</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">toggle(value)</code></li> </ul> </li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> div.classList.remove("user"); </div></pre> </td> </tr> </tbody> </table> <h4 id="焦点管理" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 焦点管理</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.activeElement</code>属性,始终会引用DOM中前端获得了焦点的元素。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var button = document.getElementById("myButton"); </div> <div class="line" style="height:20px"> button.focus(); </div> <div class="line" style="height:20px"> alert(document.activeElement === button); //true </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.hasFocus()</code>方法,可以确定文档是否获得了焦点。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var button = document.getElementById("myButton"); </div> <div class="line" style="height:20px"> button.focus(); </div> <div class="line" style="height:20px"> alert(document.hasFocus()); //true </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="HTMLDocument的变化" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">HTMLDocument</code>的变化</h4> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:20px; margin-bottom:20px; padding:0px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; text-align:justify"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; padding:0px; word-wrap:break-word; background:none">+ `readyState`属性有两个值,`loading`和`complete` </code></pre> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> if (document.readyState == 'complete') { </div> <div class="line" style="height:20px"> // 加载完成 </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <h4 id="document-charset字符集属性" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.charset</code>字符集属性</h4> <h4 id="data-自定义数据属性" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">data-</code>自定义数据属性</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <div id="myDiv" data-appId="12345" data-myname="Nicholas"></div> </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var div = document.getElementById("myDiv"); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //取得自定义属性的值 </div> <div class="line" style="height:20px"> var appId = div.dataset.appId; </div> <div class="line" style="height:20px"> var myName = div.dataset.myname; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //设置值 </div> <div class="line" style="height:20px"> div.dataset.appId = 23456; </div> <div class="line" style="height:20px"> div.dataset.myname = "Michael"; </div></pre> </td> </tr> </tbody> </table> <h4 id="innerHTML属性" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">innerHTML</code>属性</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在读模式下,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">innerHTML</code> 属性返回与调用元素的所有子节点(包括元素、注释和文本节点)对应<br> 的HTML 标记。在写模式下,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">innerHTML</code> 会根据指定的值创建新的DOM树,然后用这个DOM树完全<br> 替换调用元素原先的所有子节点</p> <h4 id="outerHTML属性" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">outerHTML</code>属性</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在读模式下,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">outerHTML</code> 返回调用它的元素及所有子节点的HTML 标签。在写模式下,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">outerHTML</code><br> 会根据指定的HTML 字符串创建新的DOM 子树,然后用这个DOM子树完全替换调用元素。</p> <h4 id="insertAdjacentHTML-方法" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">insertAdjacentHTML()</code>方法</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 插入元素的新增方法,接受两个参数,插入的位置和要插入的HTML文本,第一个参数的值:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">'beforebegin'</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">'afterbegin'</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">'beforeend'</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">'afterend'</code></li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> //作为前一个同辈元素插入 </div> <div class="line" style="height:20px"> element.insertAdjacentHTML("beforebegin", "<p>Hello world!</p>"); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //作为第一个子元素插入 </div> <div class="line" style="height:20px"> element.insertAdjacentHTML("afterbegin", "<p>Hello world!</p>"); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //作为最后一个子元素插入 </div> <div class="line" style="height:20px"> element.insertAdjacentHTML("beforeend", "<p>Hello world!</p>"); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //作为后一个同辈元素插入 </div> <div class="line" style="height:20px"> element.insertAdjacentHTML("afterend", "<p>Hello world!</p>"); </div></pre> </td> </tr> </tbody> </table> <h4 id="scrollIntoView-方法" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollIntoView()</code>方法</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollIntoView</code>方法可以在所有HTML元素上调用,通过滚动浏览器窗口或某个容器元素,调用元素就可以出现在视口中。如果这个方法传入<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code>作为参数,或者不传参数,那么窗口滚动之后就会让调用元素的顶部与视口顶部 尽可能平齐,如果传入<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">false</code>,调用元素会尽可能全部出现在视口中,不过顶部不一定平齐。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> // 让元素可见 </div> <div class="line" style="height:20px"> document.form[0].scrollIntoView(); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h3 id="专有扩展" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 专有扩展</h3> <h4 id="插入文本" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 插入文本</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">innerText</code>属性</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">outerText</code>属性</li> </ul> <h4 id="滚动" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 滚动</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollIntoViewIfNeeded(alignCenter)</code>:只有在当前元素不可见的情况下,才滚动浏览器或窗口或容器元素最终让它可见。如果当前元素在视口中可见,这个方法什么也不做。</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollByLines(lineCount)</code>:将元素的内容滚动指定的行高,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">lineCount</code>值可以是正值,也可以是负值。</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollByPages(pageCount)</code>:将元素的内容滚动指定的页面高度,具体高度由元素的高度决定。</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollIntoView()</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollIntoViewIfNeeded()</code>的作用对象是元素的容器,而<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollByLines()</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollByPages()</code>影响的则是元素自身。</p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> //在当前元素不可见的时候,让它进入浏览器的视口 </div> <div class="line" style="height:20px"> document.images[0].scrollIntoViewIfNeeded(); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //将页面主体往回滚动1 页 </div> <div class="line" style="height:20px"> document.body.scrollByPages(-1); </div></pre> </td> </tr> </tbody> </table> <h2 id="DOM2-和-DOM3" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> DOM2 和 DOM3</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="样式" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 样式</h3> <h3 id="元素大小" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 元素大小</h3> <h4 id="偏移量" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 偏移量</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetHeight</code>:元素在垂直方向上占用的空间大小。包括元素的高度,(可见的)水平滚动条的高度,上边框高度和下边框高度</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetWidth</code>:元素在水平方向上占用的空间大小。包括元素的宽度,(可见的)垂直滚动条的宽度,左边框宽度和右边框宽度</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetLeft</code>:元素的左外边框至包含元素的左内边框之间的像素距离。</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetTop</code>:元素的上外边框至包含元素的上内边框之间的像素距离。</li> </ul> <a href="http://img.e-com-net.com/image/info8/e21ee1218b1f4979974899f05dc5e817.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/e21ee1218b1f4979974899f05dc5e817.jpg" alt="JS红宝书·读书笔记_第6张图片" title="" style="border:1px solid rgb(221,221,221); display:block!important; margin:0px auto; max-width:100%; height:auto; padding:3px;border:1px solid black;" width="650" height="355"></a> <span style="color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"></span> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 其中,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetLeft</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetTop</code>属性与包含元素有关,包含元素的引用保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetParent</code>属性中。<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetParent</code>属性不一定与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">parentNode</code>的值相等。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> // 元素上偏移 </div> <div class="line" style="height:20px"> function getElementLeft (ele) { </div> <div class="line" style="height:20px"> var actualLeft = ele.offsetLeft; </div> <div class="line" style="height:20px"> var current = ele.offsetParent; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> while (current !== null) { </div> <div class="line" style="height:20px"> actualLeft += current.offsetLeft; </div> <div class="line" style="height:20px"> current = current.offsetParent; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> return actualLeft; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> // 元素左偏移同理 </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 一般来说,页面中所有的元素都被包含在几个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><div></code>元素中,而这些<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><div></code>元素的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetParent</code>又是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><body></code>元素,所以<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getElementLeft()</code>与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getElementTop()</code>会返回与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetLeft</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offsetTop</code>相同的值。</p> <h4 id="客户区大小" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 客户区大小</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientWidth</code>:元素内容区宽度加上左右内边距宽度</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientHeight</code>: 元素内容区高度加上上下内边距高度</li> </ul> <a href="http://img.e-com-net.com/image/info8/db4728d8611941bda31c232cee46d553.png" target="_blank"><img src="http://img.e-com-net.com/image/info8/db4728d8611941bda31c232cee46d553.png" alt="JS红宝书·读书笔记_第7张图片" title="" style="border:1px solid rgb(221,221,221); display:block!important; margin:0px auto; max-width:100%; height:auto; padding:3px;border:1px solid black;" width="647" height="391"></a> <span style="color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"></span> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function getViewport(){ </div> <div class="line" style="height:20px"> if (document.compatMode == "BackCompat"){ // IE7之前 </div> <div class="line" style="height:20px"> return { </div> <div class="line" style="height:20px"> width: document.body.clientWidth, </div> <div class="line" style="height:20px"> height: document.body.clientHeight </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> return { </div> <div class="line" style="height:20px"> width: document.documentElement.clientWidth, </div> <div class="line" style="height:20px"> height: document.documentElement.clientHeight </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <h4 id="滚动大小" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 滚动大小</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>滚动大小</strong>,指的是包含滚动内容的元素的大小。有些元素(<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><html></code>),即使没有执行任何代码也能自动添加滚动条;但另外一些元素,则需要通过<strong>CSS</strong>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">overflow</code>属性设置才能滚动。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollHeight</code>:在没有滚动条的情况下,元素内容的总高度</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollWidth</code>:在没有滚动条的情况下,元素内容的总宽度</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollLeft</code>:被隐藏在内容区域左侧的像素数,通过设置这个属性可以改变元素的滚动位置</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollTop</code>:被隐藏在内容区域上方的像素数,通过设置这个属性可以改变元素的滚动位置</li> </ul> <a href="http://img.e-com-net.com/image/info8/8985e0138ea7437cb2f27c8b19cf530e.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/8985e0138ea7437cb2f27c8b19cf530e.jpg" alt="JS红宝书·读书笔记_第8张图片" title="" style="border:1px solid rgb(221,221,221); display:block!important; margin:0px auto; max-width:100%; height:auto; padding:3px;border:1px solid black;" width="650" height="388"></a> <span style="color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"></span> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollWidth</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollHeight</code>主要用于确定元素内容的实际大小。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollWidth</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollHeight</code> 与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientWidth</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientHeight</code> 之间的关系?</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><strong>Firefox</strong>中这两组属性始终相等,但大小代表的是文档内容区域的实际尺寸,非视口尺寸</li> <li style="list-style:circle"><strong>Opera Safari Chrome</strong>中这两组属性有区别,其中<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollWidth</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollHeight</code> 等于视口大小,而<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientWidth</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientHeight</code> 等于文档内容区域的大小。</li> <li style="list-style:circle"><strong>IE(在标准模式)</strong>中的这两组属性不相等,其中<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollWidth</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollHeight</code> 等于文档内容区域的大小,而<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientWidth</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clientHeight</code> 等于视口大小。</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollLeft</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollTop</code> 属性既可以确定元素当前滚动的状态,也可以设置元素的滚动位置。在元素尚未被滚动时,这两个属性的值都等于0。如果元素被垂直滚动了,那么<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollTop</code> 的值会大于0,且表示元素上方不可见内容的像素高度。如果元素被水平滚动了,那么<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollLeft</code> 的值会大于0,且表示元素左侧不可见内容的像素宽度。这两个属性都是可以设置的,因此将元素的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollLeft</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scrollTop</code> 设置为0,就可以重置元素的滚动位置。下面这个函数会检测元素是否位于顶部,如果不是就将其回滚到顶部。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function scrollToTop(element){ </div> <div class="line" style="height:20px"> if (element.scrollTop != 0){ </div> <div class="line" style="height:20px"> element.scrollTop = 0; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="确定元素大小" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 确定元素大小</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getBoundingClientRect()</code>方法,会返回一个矩形对象,包含<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">left top right bottom</code>四个属性。这些属性给出了元素在页面中相对于视口的位置。</li> </ul> <h3 id="遍历" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 遍历</h3> <h4 id="NodeIterator" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> NodeIterator</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 可以使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.createNodeIterator()</code>方法创建它的新实例,接受4个参数。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">root</code>:想要作为搜索起点的树中的节点</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">whatToShow</code>:表示要访问哪些节点的数字代码</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">filter</code>:是一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter</code>对象,或者一个表示应该接受还是拒绝某种特定节点的函数</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">entityReferenceExpansion</code>:布尔值,表示是否要扩展实体引用。</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">whatToShow</code>这个参数的值以常量形式在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter</code>类型中定义:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_ALL</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_ELEMENT</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_ATTRIBUTE</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_TEXT</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_CDATA_SECTION</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_ENTITY_REFERENCE</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_ENTITYE</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_PROCESSING_INSTRUCTION</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_COMMENT</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_DOCUMENT</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_DOCUMENT_TYPE</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_DOCUMENT_FRAGMENT</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">NodeFilter.SHOW_NOTATION</code></li> </ul> <h2 id="事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> 事件</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="事件流" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 事件流</h3> <h4 id="事件冒泡" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 事件冒泡</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> IE的事件流叫做<strong>事件冒泡</strong>,即事件开始时由最具体的元素接受,然后逐级向上传播到较为不具体的节点。</p> <h4 id="事件捕获" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 事件捕获</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> Netscape 团队提出的事件流叫做<strong>事件捕获</strong>,事件捕获的用意在于在事件到达预定目标之前捕获它。</p> <h4 id="DOM事件流" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> DOM事件流</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> “DOM2级事件”规定的事件流包括三个阶段:<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">事件捕获阶段</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">处于目标阶段</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">事件冒泡阶段</code>。</p> <h3 id="事件处理程序" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 事件处理程序</h3> <h4 id="DOM0-级事件处理程序" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> DOM0 级事件处理程序</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 每个元素(包括<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">window</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document</code>)都有自己的事件处理程序,这些属性通常全部小写。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var btn = document.getElementById('myBtn'); </div> <div class="line" style="height:20px"> btn.onclick = function () { </div> <div class="line" style="height:20px"> console.log('clicked'); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> DOM 0级方法指定的事件处理程序被认为是元素的方法,因此,这个时候的事件处理程序是在元素的作用域中运行,也就是说程序中的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">this</code>可以引用当前元素。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var btn = document.getElementById('myBtn'); </div> <div class="line" style="height:20px"> btn.onclick = function () { </div> <div class="line" style="height:20px"> console.log(this.id); // 'myBtn' </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 以这种方式添加的事件处理程序会在事件流的<strong>冒泡阶段</strong>被处理。</p> <h4 id="DOM2-级事件处理程序" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> DOM2 级事件处理程序</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">addEventListener()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">removeEventListener()</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 定义了两个方法用于处理指定和删除事件处理程序的操作。所有的DOM节点中都包含这两个方法,接受三个参数:<strong>事件名</strong>、<strong>事件处理程序</strong>和<strong>布尔值</strong>。最后这个布尔值如果是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code>,表示在捕获阶段调用事件处理程序;<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">false</code>表示在冒泡阶段调用事件处理程序,默认是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">false</code>。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">addEventListener()</code>添加的事件处理程序只能使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">removeEventListener()</code>来移除。如果通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">addEventListener()</code>添加的匿名函数将无法移除。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> btn.addEventListener('click', function () { //匿名函数 </div> <div class="line" style="height:20px"> ... </div> <div class="line" style="height:20px"> }) </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>注</strong>:大多数情况下,都是将事件处理程序添加到事件流的冒泡阶段(<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">false</code>),这样可以最大限度地兼容各种浏览器。最好只在需要在事件到达目标之前截获它的时候将事件处理程序添加到捕获阶段。如果不是特别需要,我们不建议在事件捕获阶段注册事件处理程序。</p> <h4 id="IE事件处理程序" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> IE事件处理程序</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">attachEvent()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">detachEvent()</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 这两个方法接受两个参数:事件名(带<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">on</code>)和事件处理函数。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var btn = document.getElementById("myBtn"); </div> <div class="line" style="height:20px"> btn.attachEvent("onclick", function(){ </div> <div class="line" style="height:20px"> alert("Clicked"); </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>注</strong>:在IE 中使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">attachEvent()</code>与使用DOM0 级方法的主要区别在于事件处理程序的作用域。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle">DOM0 级作用域是其所属元素</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">attachEvent()</code>方法的作用域是全局(<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">this === window</code>)</li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var btn = document.getElementById("myBtn"); </div> <div class="line" style="height:20px"> btn.attachEvent("onclick", function(){ </div> <div class="line" style="height:20px"> alert("Clicked"); </div> <div class="line" style="height:20px"> }); </div> <div class="line" style="height:20px"> btn.attachEvent("onclick", function(){ </div> <div class="line" style="height:20px"> alert("Hello world!"); </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>注</strong>:与DOM方法不同的是,这些事件处理程序不是以添加它们的顺序执行,而是以相反的顺序被触发。单击这个例子中的按钮,首先看到的是”Hello world!”,然后才是”Clicked”。</p> <h3 id="事件对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 事件对象</h3> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在触发DOM上的某个事件时,会产生一个事件对象<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event</code>。</p> <h4 id="DOM中的事件对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> DOM中的事件对象</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event</code>对象成员</p> <table style="border-collapse:collapse; border-spacing:0px; margin:20px 0px; width:619px; border:1px solid rgb(221,221,221); font-size:14px; table-layout:fixed; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; text-align:justify"> <thead> <tr> <th style="padding:8px 8px 10px; text-align:left; vertical-align:middle; border-bottom:3px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 属性/方法</th> <th style="padding:8px 8px 10px; text-align:left; vertical-align:middle; border-bottom:3px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 类型</th> <th style="padding:8px 8px 10px; text-align:left; vertical-align:middle; border-bottom:3px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 读/写</th> <th style="padding:8px 8px 10px; text-align:left; vertical-align:middle; border-bottom:3px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 说明</th> </tr> </thead> <tbody> <tr style="background-color:rgb(249,249,249)"> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">bubbles</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Boolean</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 表明事件是否冒泡</td> </tr> <tr> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">cancelable</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Boolean</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 表明是否可以取消事件的默认行为</td> </tr> <tr style="background-color:rgb(249,249,249)"> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">currentTarget</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 其事件处理程序当前正在处理事件的那个元素</td> </tr> <tr> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">defaultPrevented</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Boolean</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code>表示已经调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">preventDefault()</code></td> </tr> <tr style="background-color:rgb(249,249,249)"> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">detail</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Integer</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 与事件相关的细节信息</td> </tr> <tr> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">eventPhase</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Integer</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 调用事件处理程序的阶段:1 捕获,2 处于目标,3 冒泡</td> </tr> <tr style="background-color:rgb(249,249,249)"> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">preventDefault()</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Function</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 取消事件的默认行为。如果<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">cancelable</code>是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code>,则可以使用这个方法</td> </tr> <tr> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">stopImmediatePropagation()</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Function</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 取消事件的进一步冒泡或捕获,同时阻止任何事件处理程序被调用</td> </tr> <tr style="background-color:rgb(249,249,249)"> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">stopPropagation()</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Function</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 取消事件的进一步捕获或冒泡。如果<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">bubbles</code>为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code>,则可以使用这个方法</td> </tr> <tr> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">target</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Element</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 事件的目标</td> </tr> <tr style="background-color:rgb(249,249,249)"> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">trusted</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Boolean</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code>表示事件是浏览器生成,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">false</code>是开发人员创建</td> </tr> <tr> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">type</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">String</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 被触发的事件类型</td> </tr> <tr style="background-color:rgb(249,249,249)"> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">view</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">AbstractView</code></td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 只读</td> <td style="padding:8px; text-align:left; vertical-align:middle; border-bottom:1px solid rgb(221,221,221); border-right:1px solid rgb(238,238,238)"> 与事件关联的抽象视图。等同于发生事件的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">window</code>对象</td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在事件处理程序内部,对象<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">this</code> 始终等于<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">currentTarget</code> 的值,而<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">target</code> 则只包含事件的实际目标。如果直接将事件处理程序指定给了目标元素,则<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">this</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">currentTarget</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">target</code> 包含相同的值。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> document.body.onclick = function(event){ </div> <div class="line" style="height:20px"> alert(event.currentTarget === document.body); //true </div> <div class="line" style="height:20px"> alert(this === document.body); //true </div> <div class="line" style="height:20px"> alert(event.target === document.getElementById("myBtn")); //true </div> <div class="line" style="height:20px"> }; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event</code>方法<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var link = document.getElementById("myLink"); </div> <div class="line" style="height:20px"> link.onclick = function(event){ </div> <div class="line" style="height:20px"> event.preventDefault(); </div> <div class="line" style="height:20px"> }; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="跨浏览器的事件对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 跨浏览器的事件对象</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div> <div class="line" style="height:20px"> 23 </div> <div class="line" style="height:20px"> 24 </div> <div class="line" style="height:20px"> 25 </div> <div class="line" style="height:20px"> 26 </div> <div class="line" style="height:20px"> 27 </div> <div class="line" style="height:20px"> 28 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var EventUtil = { </div> <div class="line" style="height:20px"> addHandler: function(element, type, handler){ </div> <div class="line" style="height:20px"> //省略的代码 </div> <div class="line" style="height:20px"> }, </div> <div class="line" style="height:20px"> getEvent: function(event){ </div> <div class="line" style="height:20px"> return event ? event : window.event; </div> <div class="line" style="height:20px"> }, </div> <div class="line" style="height:20px"> getTarget: function(event){ </div> <div class="line" style="height:20px"> return event.target || event.srcElement; </div> <div class="line" style="height:20px"> }, </div> <div class="line" style="height:20px"> preventDefault: function(event){ </div> <div class="line" style="height:20px"> if (event.preventDefault){ </div> <div class="line" style="height:20px"> event.preventDefault(); </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> event.returnValue = false; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }, </div> <div class="line" style="height:20px"> removeHandler: function(element, type, handler){ </div> <div class="line" style="height:20px"> //省略的代码 </div> <div class="line" style="height:20px"> }, </div> <div class="line" style="height:20px"> stopPropagation: function(event){ </div> <div class="line" style="height:20px"> if (event.stopPropagation){ </div> <div class="line" style="height:20px"> event.stopPropagation(); </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> event.cancelBubble = true; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }; </div></pre> </td> </tr> </tbody> </table> <h3 id="事件类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 事件类型</h3> <h4 id="UI-事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> UI 事件</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">load</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">unload</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">resize</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scroll</code>事件</li> </ul> <h4 id="焦点事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 焦点事件</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">blur</code>事件:失去焦点</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">focus</code>事件:获得焦点</li> </ul> <h4 id="鼠标与滚动事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 鼠标与滚动事件</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">click</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dbclick</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousedown</code>事件:按下鼠标</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseenter</code>事件:光标移入</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseleave</code>事件:光标移出</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousemove</code>事件:鼠标在元素内部移动重复触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseout</code>事件:在鼠标指针位于一个元素上方,然后用户将其移入另一个元素时触发。又移入的另一个元素可能位于前一个元素的外部,也可能是这个元素的子元素</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseover</code>事件:在鼠标指针位于一个元素外部,然后用户将其首次移入另一个元素边界之内时触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseup</code>事件:释放鼠标按钮时触发</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 页面上的所有元素都支持鼠标事件。除了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseenter</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseleave</code>,所有鼠标事件都会冒泡,也可以被取消,而取消鼠标事件将会影响浏览器的默认行为。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 只有在同一个元素上相继触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousedown</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseup</code> 事件,才会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">click</code> 事件;如果<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousedown</code>或<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseup</code> 中的一个被取消,就不会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">click</code> 事件。</p> <h4 id="触摸设备" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 触摸设备</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> iOS和Android设备的相关事件:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle">不支持<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dbclick</code>事件。双击浏览器窗口会放大画面</li> <li style="list-style:circle">轻击可单击元素会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousemove</code>事件。。如果此操作会导致内容变化,将不再有其他事件发生;如果屏幕没有因此变化,那么会依次发生<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousedown</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseup</code> 和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">click</code> 事件。轻击不可单击的元素不会触发任何事件。可单击的元素是指那些单击可产生默认操作的元素(如链接),或者那些已经被指定了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onclick</code> 事件处理程序的元素。</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousemove</code>事件也会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseover</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mouseout</code>事件</li> <li style="list-style:circle">两个手指放在屏幕上且页面随手指移动而滚动时会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">mousewheel</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">scroll</code>事件。</li> </ul> <h4 id="HTML5事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> HTML5事件</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">contextmenu</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">beforeunload</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">DOMContentLoaded</code>事件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readystatechange</code>事件 <ul style="list-style:none"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">uninitialized</code>未初始化</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">loading</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">loaded</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">interactive</code>:可以操作对象,但还没有完全加载</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">complete</code></li> </ul> </li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">hashchange</code>事件</li> </ul> <h4 id="设备事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 设备事件</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">orientationchange</code>事件:横竖屏,有三个值: -90 ,0, 90</li> </ul> <h4 id="触摸与手势事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 触摸与手势事件</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle">触摸事件 <ul style="list-style:none"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">touchstart</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">touchmove</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">touchend</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">touchcancel</code></li> </ul> </li> <li style="list-style:circle">手势事件 <ul style="list-style:none"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">gesturestart</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">gesturechange</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">gestureend</code></li> </ul> </li> </ul> <h3 id="内存和性能" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 内存和性能</h3> <h4 id="事件委托" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 事件委托</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 例如在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><ul></code>为添加一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">click</code>事件,所有<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><li></code>子元素点击事件都会冒泡到<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><ul></code>上。</p> <h2 id="表单脚本" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> 表单脚本</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="表单基础知识" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 表单基础知识</h3> <h4 id="提交表单" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 提交表单</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <input type="submit" value="Submit Form"> </div></pre> </td> </tr> </tbody> </table> <h4 id="重置表单" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 重置表单</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <input type="reset" value="Reset Form"> </div></pre> </td> </tr> </tbody> </table> <h4 id="表单字段" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 表单字段</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 每个表单都有<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">elements</code>属性,该属性是表单中所有表单元素的集合。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var form = document.getElementById("form1"); </div> <div class="line" style="height:20px"> //取得表单中的第一个字段 </div> <div class="line" style="height:20px"> var field1 = form.elements[0]; </div> <div class="line" style="height:20px"> //取得名为"textbox1"的字段 </div> <div class="line" style="height:20px"> var field2 = form.elements["textbox1"]; </div> <div class="line" style="height:20px"> //取得表单中包含的字段的数量 </div> <div class="line" style="height:20px"> var fieldCount = form.elements.length; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h3 id="文本框脚本" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 文本框脚本</h3> <h4 id="过滤输入" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 过滤输入</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 屏蔽特定的字符,需要检测<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">keypress</code>事件对应的字符编码。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> EventUtil.addHandler(textbox, 'keypress', function (event) { </div> <div class="line" style="height:20px"> event = EventUtil.getEvent(event); </div> <div class="line" style="height:20px"> var target = EventUtil.getTarget(event); </div> <div class="line" style="height:20px"> var charCode = EventUtil.getCharCode(event); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> if (!/\d/.test(String.fromCharCode(charCode))) { </div> <div class="line" style="height:20px"> EventUtil.preventDefault(event); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }) </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="HTML5约束验证API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> HTML5约束验证API</h4> <h5 id="输入模式" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 输入模式</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> HTML5为文本字段新增了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">pattern</code>属性。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <input type="text" pattern="\d+" name="count"> </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="检测有效性" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 检测有效性</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">checkValidity()</code>方法可以检测表单中的某个字段是否有效。是否有效的判断依据是一些<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><input></code>的约束条件。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> if (document.forms[0].elements[0].checkValidity()){ </div> <div class="line" style="height:20px"> //字段有效,继续 </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> //字段无效 </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 也可以检测整个表单是否有效<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> if(document.forms[0].checkValidity()){ </div> <div class="line" style="height:20px"> //表单有效,继续 </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> //表单无效 </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="禁用验证" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 禁用验证</h5> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <form method="post" action="signup.php" novalidate> </div> <div class="line" style="height:20px"> <!--这里插入表单元素--> </div> <div class="line" style="height:20px"> </form> </div></pre> </td> </tr> </tbody> </table> <h2 id="HTML5-脚本编程" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> HTML5 脚本编程</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="跨文档消息传递" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 跨文档消息传递</h3> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>跨文档消息传送</strong>(cross-document messaging)简称XDM。其核心方法是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">postMessage()</code>方法。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">postMessage()</code>方法接受两个参数:一条消息和一个表示消息接收方来自哪个域的字符串。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> // 注意:所有支持XDM的浏览器也支持iframe的`contentWindow`属性 </div> <div class="line" style="height:20px"> var iframeWindow = document.getElementById('myframe').contentWindow; </div> <div class="line" style="height:20px"> iframeWindow.postMessage('A secret', 'https://yeasoenzhang.github.io'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 尝试向内嵌框架中发送一条消息,并指定框架中的文档必须来源于<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">https://yeasonzhang.github.io</code>域。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 接收到<strong>XDM</strong>消息时,会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">window</code>对象的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">message</code>事件,这个事件是以异步形式触发。<br> 传递的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onmessage</code>处理程序的事件对象包含三个重要信息:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">data</code>:作为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">postMessage()</code>第一个参数传入的字符串数据</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">origin</code>:发送消息的文档所在的域。</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">source</code>:发送消息的文档的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">window</code>对象的代理。</li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> EventUtil.addHandler(window, "message", function(event){ </div> <div class="line" style="height:20px"> //确保发送消息的域是已知的域 </div> <div class="line" style="height:20px"> if (event.origin == "https://yeasonzhang.github.io"){ </div> <div class="line" style="height:20px"> //处理接收到的数据 </div> <div class="line" style="height:20px"> processMessage(event.data); </div> <div class="line" style="height:20px"> //可选:向来源窗口发送回执 </div> <div class="line" style="height:20px"> event.source.postMessage("Received!", "http://p2p.wrox.com"); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> XDM 还有一些怪异之处。首先,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">postMessage()</code>的第一个参数最早是作为“永远都是字符串”来实现的。但后来这个参数的定义改了,改成允许传入任何数据结构。可是,并非所有浏览器都实现了这一变化。为保险起见,使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">postMessage()</code>时,最好还是只传字符串。如果你想传入结构化的数据,最佳选择是先在要传入的数据上调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON.stringify()</code>,通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">postMessage()</code>传入得到的字符串,然后再在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onmessage</code> 事件处理程序中调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON.parse()</code>。</p> <h3 id="原生拖放" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 原生拖放</h3> <h4 id="拖放事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 拖放事件</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 拖动某个元素时,将依次触发的事件:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dragstart</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">drag</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dragend</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 当某个元素被拖动到一个有效的放置目标时,会依次触发下列事件:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dragenter</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dragover</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dragleave</code>(离开)或<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">drag</code>(放进去了)</li> </ul> <h4 id="dataTransfer对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> dataTransfer对象</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dataTransfer</code>对象,它是事件对象的一个属性,用于被拖动元素向放置目标传递<strong>字符串格式</strong>的数据。该对象有两个主要方法:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getData()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setData()</code> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> //设置和接收文本数据 </div> <div class="line" style="height:20px"> event.dataTransfer.setData("text", "some text"); </div> <div class="line" style="height:20px"> var text = event.dataTransfer.getData("text"); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //设置和接收URL </div> <div class="line" style="height:20px"> event.dataTransfer.setData("URL", "http://www.wrox.com/"); </div> <div class="line" style="height:20px"> var url = event.dataTransfer.getData("URL"); </div></pre> </td> </tr> </tbody> </table> </li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 不过,保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dataTransfer</code>对象中的数据只能在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">drap</code>事件处理程序中读取。如果在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ondrop</code> 处理程序中没有读到数据,那就是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dataTransfer</code> 对象已经被销毁,数据也丢失了。</p> <h4 id="drapEffect-与-effectAllowed" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> drapEffect 与 effectAllowed</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dateTransfer</code>对象有两个属性:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">effectAllowed</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code>,属性可以知道被拖动的元素能够执行那种放置行为。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">none</code>:不能放在这里</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">move</code>:应该把拖放的元素移动到放置目标</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">copy</code>:应该把拖动的元素复制到放置目标</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">link</code>:表示放置目标会打开拖动的元素</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 要使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code>属性,必须在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ondragenter</code>事件处理程序中针对放置目标来设置。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">effectAllowed</code>属性表示允许拖动元素的哪种<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code>。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">uninitialized</code>:没有给被拖动的元素放置任何放置行为</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">none</code>:被拖动的元素不能有任何行为</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">copy</code>:只允许值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">copy</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">link</code>:只允许值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">link</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">move</code>:只允许值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">move</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">copyLink</code>:允许值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">copy</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">link</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">copyMove</code>:允许值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">copy</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">move</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">linkMove</code>:允许值为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">link</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">move</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">all</code>: 允许任意<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dropEffect</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 必须在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ondragstart</code> 事件处理程序中设置<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">effectAllowed</code> 属性。</p> <h4 id="可拖动" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 可拖动</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> HTML5为所有元素规定了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">draggable</code>属性,表示元素是否可以拖动。只有图像和链接的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">draggable</code>默认是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">true</code><br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> <!-- 让这个图像不可以拖动 --> </div> <div class="line" style="height:20px"> <img src="smile.gif" draggable="false" alt="Smiley face"> </div> <div class="line" style="height:20px"> <!-- 让这个元素可以拖动 --> </div> <div class="line" style="height:20px"> <div draggable="true">...</div> </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="其他成员" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 其他成员</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> HTML5规定了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">dateTransfer</code>对象还应该包含下列方法和属性。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">addElement(element)</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clearData(format)</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setDragImage(element, x, y)</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">type</code></li> </ul> <h2 id="错误处理与调试" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> 错误处理与调试</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="错误处理" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 错误处理</h3> <h4 id="try-catch-语句" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> try-catch 语句</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> try { </div> <div class="line" style="height:20px"> // 可能出错的代码 </div> <div class="line" style="height:20px"> } catch (err) { </div> <div class="line" style="height:20px"> // 处理发生的错误 </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <h5 id="finally子句" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">finally</code>子句</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 只要代码中包含<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">finially</code>子句,无论<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">try</code>还是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">catch</code>语句中的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">return</code>语句都将被忽略。</p> <h5 id="错误类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 错误类型</h5> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Error</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">EvalError</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">RangeError</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ReferenceError</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">SyntaxError</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">TypeError</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">URIError</code></li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> try { </div> <div class="line" style="height:20px"> someFunction(); </div> <div class="line" style="height:20px"> } catch (error) { </div> <div class="line" style="height:20px"> if (error instanceof TypeError) { </div> <div class="line" style="height:20px"> //... </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> // </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <h4 id="抛出错误" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 抛出错误</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">try-catch</code> 语句相配的还有一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">throw</code> 操作符,用于随时抛出自定义错误。抛出错误时,必须要给<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">throw</code> 操作符指定一个值,这个值是什么类型,没有要求。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> throw 12345; </div> <div class="line" style="height:20px"> throw "Hello world!"; </div> <div class="line" style="height:20px"> throw true; </div> <div class="line" style="height:20px"> throw { name: "JavaScript"}; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 遇到<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">throw</code>操作符时,代码会立即停止执行。只有当<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">try-catch</code>语句捕获到被抛出值,代码才会继续执行</p> <h5 id="自定义错误类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 自定义错误类型</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 可以利用原型链通过继承<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Error</code>创建自定义错误类型。需要为新创建的错误类型指定<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">name</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">message</code>属性<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function CustomError (message) { </div> <div class="line" style="height:20px"> this.name = 'CustomError'; </div> <div class="line" style="height:20px"> this.message = message; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> CustomError.prototype = new Error(); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> throw new CustomError('Error msg'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="Error事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> Error事件</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 任何没有通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">try-catch</code>处理的错误都会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">window</code>对象的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>事件。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在任何Web浏览器中,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onerror</code>事件处理程序都不会创建<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event</code>对象,但它可以接受三个参数:错误消息、错误所在的URL和行号。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 要指定onerror 事件处理程序,必须使用如下所示的DOM0 级技术,它没有遵循“DOM2 级事件”的标准格式(<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">addEventListener</code>)。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> window.onerror = function(message, url, line){ </div> <div class="line" style="height:20px"> alert(message); </div> <div class="line" style="height:20px"> }; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 只要发生错误,无论是不是浏览器生成的,都会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>事件,然后让浏览器的默认机制发挥作用,这时候我们需要阻止浏览器的默认行为(<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">return false</code>)。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> window.onerror = function (message, url, line) { </div> <div class="line" style="height:20px"> console.log(message); </div> <div class="line" style="height:20px"> retrun false; </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="常见的错误类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 常见的错误类型</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle">类型转换错误</li> <li style="list-style:circle">数据类型错误</li> <li style="list-style:circle">通信错误</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在数据检测的时候,基本类型的值应该使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">typeof</code>来检测,对象的值应该使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">instanceof</code>。</p> <h2 id="JSON" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> JSON</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="解析与序列化" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 解析与序列化</h3> <h4 id="JSON对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> JSON对象</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON</code>对象有两个方法:<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">stringify</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">parse()</code>。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var book = { </div> <div class="line" style="height:20px"> title: "Professional JavaScript", </div> <div class="line" style="height:20px"> authors: [ </div> <div class="line" style="height:20px"> "Nicholas C. Zakas" </div> <div class="line" style="height:20px"> ], </div> <div class="line" style="height:20px"> edition: 3, </div> <div class="line" style="height:20px"> year: 2011 </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> var jsonText = JSON.stringify(book); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 以上就把<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Javascript</code>对象序列化为一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON</code>字符串(没有空格和缩进)<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> {"title":"Professional JavaScript","authors":["Nicholas C. Zakas"],"edition":3,"year":2011} </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 如果传给<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON.parse()</code>的字符串不是有效的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON</code>,会抛出错误。</p> <h4 id="序列化选项" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 序列化选项</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON.stringify()</code>除了要序列化的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JS</code>对象外,还可以接受两个参数,一个是过滤器(数组或函数),第二个参数是一个选项,表示是都在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON</code>字符串中保留缩进。</p> <h5 id="过滤结果" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 过滤结果</h5> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var book = { </div> <div class="line" style="height:20px"> "title": "Professional JavaScript", </div> <div class="line" style="height:20px"> "authors": [ </div> <div class="line" style="height:20px"> "Nicholas C. Zakas" </div> <div class="line" style="height:20px"> ], </div> <div class="line" style="height:20px"> edition: 3, </div> <div class="line" style="height:20px"> year: 2011 </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> var jsonText = JSON.stringify(book, ["title", "edition"]); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 第二个参数中包含两个字符串<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">"title", "edition"</code>,所以只会返回对应的属性<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> {"title":"Professional JavaScript","edition":3} </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 过滤器为函数<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var book = { </div> <div class="line" style="height:20px"> "title": "Professional JavaScript", </div> <div class="line" style="height:20px"> "authors": [ </div> <div class="line" style="height:20px"> "Nicholas C. Zakas" </div> <div class="line" style="height:20px"> ], </div> <div class="line" style="height:20px"> edition: 3, </div> <div class="line" style="height:20px"> year: 2011 </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> var jsonText = JSON.stringify(book, function(key, value){ </div> <div class="line" style="height:20px"> switch(key){ </div> <div class="line" style="height:20px"> case "authors": </div> <div class="line" style="height:20px"> return value.join(",") </div> <div class="line" style="height:20px"> case "year": </div> <div class="line" style="height:20px"> return 5000; </div> <div class="line" style="height:20px"> case "edition": </div> <div class="line" style="height:20px"> return undefined; </div> <div class="line" style="height:20px"> default: </div> <div class="line" style="height:20px"> return value; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>注</strong>:返回<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">undefined</code>删除该属性,上例的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">edition</code>属性就会被删除。</p> <h5 id="字符串缩进" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 字符串缩进</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON.stringify()</code>方法的第三个参数用于控制结果中的缩进和空白符。可以是数字,表示缩进的空格数;也可以是字符串,将该字符串作为缩进的表示。</p> <h5 id="toJSON-方法" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">toJSON()</code>方法</h5> <h4 id="解析选项" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 解析选项</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON.parse()</code>方法也可以接受第二参数,该参数是一个函数(被称为还原函数),传入函数的参数均为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">key, value</code>。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 如果还原函数返回<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">undefined</code>,则表示要从结果中删除响应的键。</p> <h2 id="Ajax与Comet" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> Ajax与Comet</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="XMLHttpRequest-对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> XMLHttpRequest 对象</h3> <h4 id="XHR的用法" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> XHR的用法</h4> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">open('method', 'url', bool)</code>:第三个参数表示是否异步发送</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">send()</code>:接受一个参数作为请求主体发送的数据,如果不需要则传入<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">null</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>对象的属性</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">responseText</code>:作为相应主体被返回的文本</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">responseXML</code>:如果相应的内容类型是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">"text/xml"</code>或<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">"application/xml"</code>,这个属性中将包含这响应数据的XML DOM文档</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">status</code>:响应的HTTP状态</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">statusText</code>:HTTP状态的说明</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 同步请求<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> xhr.open("get", "example.txt", false); </div> <div class="line" style="height:20px"> xhr.send(null); </div> <div class="line" style="height:20px"> if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ </div> <div class="line" style="height:20px"> alert(xhr.responseText); </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> alert("Request was unsuccessful: " + xhr.status); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readyState</code>:表示请求/响应过程的阶段 <ul style="list-style:none"> <li style="list-style:circle">0:未初始化,尚未调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">open()</code>方法</li> <li style="list-style:circle">1:启动,调用了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">open()</code>方法,尚未调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">send()</code>方法</li> <li style="list-style:circle">2:发送,调用了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">send()</code>方法,尚未接收到响应。</li> <li style="list-style:circle">3:接收,接收到部分响应数据</li> <li style="list-style:circle">4:完成,已经接收到全部响应数据</li> </ul> </li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xhr = createXHR(); </div> <div class="line" style="height:20px"> xhr.onreadystatechange = function(){ </div> <div class="line" style="height:20px"> if (xhr.readyState == 4){ </div> <div class="line" style="height:20px"> if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ </div> <div class="line" style="height:20px"> alert(xhr.responseText); </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> alert("Request was unsuccessful: " + xhr.status); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xhr.open("get", "example.txt", true); </div> <div class="line" style="height:20px"> xhr.send(null); </div></pre> </td> </tr> </tbody> </table> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">abort()</code>:在接收到响应之前通过该方法取消异步请求。<br> 建议调用这个方法之后,对<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>对象进行解引用操作。</li> </ul> <h4 id="HTTP-头部信息" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> HTTP 头部信息</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 默认情况下,在发送<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>请求的同时,还会发送下列头部信息:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Accept</code>:浏览器能够处理的内容类型</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Accept-Charset</code>:浏览器能够显示的字符集</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Accept-Encoding</code>:浏览器能够处理的压缩编码</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Accept-Language</code>:浏览器当前设置的语言</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Connection</code>:浏览器与服务器之间连接的类型</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Cookie</code>: 当前页面的 Cookie</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Host</code>:发出请求的页面所在的域</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Referer</code>:发出请求的页面的URI</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">User-Agent</code>:浏览器的用户代理</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 自定义请求头部信息,使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setRequestHeader()</code>方法,该方法接受两个参数:头部字段的名称和头部字段的值。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 要成功发送请求头部信息,必须在调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">open()</code>方法之后且调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">send()</code>方法之前调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">serRequestHeader()</code>。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xhr = createXHR(); </div> <div class="line" style="height:20px"> xhr.onreadystatechange = function(){ </div> <div class="line" style="height:20px"> // ... </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xhr.open("get", "example.php", true); </div> <div class="line" style="height:20px"> xhr.setRequestHeader("MyHeader", "MyValue"); </div> <div class="line" style="height:20px"> xhr.send(null); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>注</strong>建议使用自定义的头部字段名称,不要使用浏览器正常发送的字段名称,否则有可能会影响服务器的响应。有的浏览器允许开发人员重写默认的头部信息,但有的浏览器则不允许这样做。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>对象的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getResponseHeader()</code>方法,接受一个参数:头部字段名称。就能取得相应的响应头部信息。<br> 调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getAllResponseHeaders()</code>方法可以取得包含所有头部信息的字符串。</p> <h4 id="GET请求" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> GET请求</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">GET</code>请求经常会发生一个错误,就是查询字符串的格式有问题。查询字符串中每个参数的名称和值都必须使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">encodeURIComponent()</code>进行编码,然后才能放到URL的末尾。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function addURLParam(url, name, value) { </div> <div class="line" style="height:20px"> url += (url.indexOf("?") == -1 ? "?" : "&"); </div> <div class="line" style="height:20px"> url += encodeURIComponent(name) + "=" + encodeURIComponent(value); </div> <div class="line" style="height:20px"> return url; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var url = "example.php"; </div> <div class="line" style="height:20px"> //添加参数 </div> <div class="line" style="height:20px"> url = addURLParam(url, "name", "Nicholas"); </div> <div class="line" style="height:20px"> url = addURLParam(url, "book", "Professional JavaScript"); </div> <div class="line" style="height:20px"> //初始化请求 </div> <div class="line" style="height:20px"> xhr.open("get", url, false); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="POST请求" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> POST请求</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 如果我们希望用XHR模仿表单提交,需要将<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Content-Type</code>头部信息设置为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">application/x-www-form-urlencoded</code>(表单提交的内容类型)<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function submitData(){ </div> <div class="line" style="height:20px"> var xhr = createXHR(); </div> <div class="line" style="height:20px"> xhr.onreadystatechange = function(){ </div> <div class="line" style="height:20px"> if (xhr.readyState == 4){ </div> <div class="line" style="height:20px"> if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ </div> <div class="line" style="height:20px"> alert(xhr.responseText); </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> alert("Request was unsuccessful: " + xhr.status); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xhr.open("post", "postexample.php", true); </div> <div class="line" style="height:20px"> xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); </div> <div class="line" style="height:20px"> var form = document.getElementById("user-info"); </div> <div class="line" style="height:20px"> xhr.send(serialize(form)); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h3 id="XMLHttpRequest-2-级" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> XMLHttpRequest 2 级</h3> <h4 id="FormData" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> FormData</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FormData</code>为序列化表单以及创建于表单格式相同的数据提供了便利。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var data = new FormData(); </div> <div class="line" style="height:20px"> data.append('name', 'Yeaseon'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">append</code>方法可以将表单的字段和值,传入<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FormData</code>对象中。也可以预先填入表单中的字段:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var data = new FormData(document.form[0]); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FormData</code>的方便就在于不必手动修改XHR对象的请求头部。</p> <h4 id="超时设定" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 超时设定</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>对象添加了一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">timeout</code>属性,表示请求在等待多少毫秒之后终止。如果规定时间内浏览器没有收到响应,就会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">timeout</code>事件,进而调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ontimeout</code>事件处理程序。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xhr = createXHR(); </div> <div class="line" style="height:20px"> xhr.onreadystatechange = function(){ </div> <div class="line" style="height:20px"> // ... </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xhr.open("get", "timeout.php", true); </div> <div class="line" style="height:20px"> xhr.timeout = 1000; //将超时设置为1 秒钟 </div> <div class="line" style="height:20px"> xhr.ontimeout = function(){ </div> <div class="line" style="height:20px"> alert("Request did not return in a second."); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xhr.send(null); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 超时之后请求终止,但是此时的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readyState</code>可能已经变为了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">4</code>,就意味着会调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onreadystatechange</code>事件。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 可是,如果在超时终止请求之后再访问<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">status</code> 属性,就会导致错误。为避免浏览器报告错误,可以将检查<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">status</code> 属性的语句封装在一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">try-catch</code>语句当中。</p> <h4 id="overrideMimeType-方法" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> overrideMimeType()方法</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 用于重写<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>响应的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">MIME</code>类型。因为返回响应的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">MIME</code> 类型决定了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code> 对象如何处理它,所以提供一种方法能够重写服务器返回的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">MIME</code> 类型是很有用的。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xhr = createXHR(); </div> <div class="line" style="height:20px"> xhr.open("get", "text.php", true); </div> <div class="line" style="height:20px"> xhr.overrideMimeType("text/xml"); </div> <div class="line" style="height:20px"> xhr.send(null); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="进度事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 进度事件</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 有以下6个进度事件:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">loadstart</code>:在接收到响应数据的第一个字节触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">progress</code>:在接收响应期间持续不断地触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>:在请求发生错误时触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">abort</code>:在因为调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">abort()</code>方法而终止连接时触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">load</code>:在接收到完整的响应数据时触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">loadend</code>:在通信完成或者触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>、<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">abort</code>,或<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">load</code>事件后触发</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>progress</strong>事件<br> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onprogress</code>事件处理程序会接收到一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event</code>对象,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">target</code>属性指向<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>对象,包含着三个额外的属性:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">lengthComputable</code>:表示进度信息是否可用的布尔值</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">position</code>:表示已经接受的字节数</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">totalSize</code>:表示根据<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Content-Length</code>响应头部确定的预期字节数。</li> </ul> <h3 id="跨资源共享" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 跨资源共享</h3> <h4 id="IE对CORS的实现" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> IE对CORS的实现</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 微软在IE8中引入了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XDR</code>类型,类似与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>对象,两者的不同之处:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle">cookie不会随请求发送,也不会随响应返回</li> <li style="list-style:circle">只能设置请求头部信息中的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Content-Type</code>字段</li> <li style="list-style:circle">不能访问响应头部信息</li> <li style="list-style:circle">只支持<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">GET</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">POST</code>请求</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 请求返回之后,就会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">load</code>事件,响应数据也会保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">responseText</code>属性中:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xdr = new XDomainRequest(); </div> <div class="line" style="height:20px"> xdr.onload = function () { </div> <div class="line" style="height:20px"> console.log(xdr.responseText); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xdr.onerror = function(){ </div> <div class="line" style="height:20px"> alert("An error occurred."); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xdr.open('get', 'http://..../xxx/'); </div> <div class="line" style="height:20px"> xdr.send(null); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在请求返回之前可以调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">abort()</code>方法终止请求。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> xdr.abort(); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XDR</code>对象也支持<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">timeout</code>属性以及<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ontimeout</code>事件处理程序<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xdr = new XDomainRequest(); </div> <div class="line" style="height:20px"> xdr.onload = function(){ </div> <div class="line" style="height:20px"> alert(xdr.responseText); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xdr.onerror = function(){ </div> <div class="line" style="height:20px"> alert("An error occurred."); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xdr.timeout = 1000; </div> <div class="line" style="height:20px"> xdr.ontimeout = function(){ </div> <div class="line" style="height:20px"> alert("Request took too long."); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xdr.open("get", "http://www.somewhere-else.com/page/"); </div> <div class="line" style="height:20px"> xdr.send(null); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 为了支持<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">POST</code>请求,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XDR</code>对象提供了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">contentType</code>属性,用来表示发送数据的格式。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xdr = new XDomainRequest(); </div> <div class="line" style="height:20px"> xdr.onload = function () { </div> <div class="line" style="height:20px"> // </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> xdr.onerror = function () { </div> <div class="line" style="height:20px"> // </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> xdr.open('post', 'http://www.somewhere-else.com/page/'); </div> <div class="line" style="height:20px"> xdr.contentType = 'application/x-www-form-urlencoded'; </div> <div class="line" style="height:20px"> xdr.send('name1=value1&name2=value2'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="其他浏览器对CORS的实现" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 其他浏览器对CORS的实现</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 与IE中的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XDR</code>对象不同,通过跨域<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>对象可以访问<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">status</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">statusText</code>属性,并且支持同步请求。同时也有一些限制:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle">不能使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setRequestHeader()</code>设置自定义头部</li> <li style="list-style:circle">不能发送和接收<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">cookie</code></li> <li style="list-style:circle">调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getAllResponseHeaders()</code>方法总会返回空字符串</li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var xhr = createXHR(); </div> <div class="line" style="height:20px"> xhr.onreadystatechange = function(){ </div> <div class="line" style="height:20px"> if (xhr.readyState == 4){ </div> <div class="line" style="height:20px"> if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ </div> <div class="line" style="height:20px"> alert(xhr.responseText); </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> alert("Request was unsuccessful: " + xhr.status); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> xhr.open("get", "http://www.somewhere-else.com/page/", true); </div> <div class="line" style="height:20px"> xhr.send(null); </div></pre> </td> </tr> </tbody> </table> <h3 id="其他跨域技术" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 其他跨域技术</h3> <h4 id="图像Ping" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 图像Ping</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var img = new Image(); </div> <div class="line" style="height:20px"> img.onload = img.onerror = function(){ </div> <div class="line" style="height:20px"> alert("Done!"); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> img.src = "http://www.example.com/test?name=Nicholas"; </div></pre> </td> </tr> </tbody> </table> <h4 id="JSONP" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> JSONP</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSONP</code>是<strong>JSON with padding</strong>的简写。<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSONP</code>只不过时被包含在函数调用中的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSON</code>:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> callback({"name": "Yeaseon"}); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSONP</code>由两部分组成:回调函数和数据。回调函数是当响应到来时应该在页面中调用的函数。回调函数的名字一般是请求中指定的。下面是一个经典的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSONP</code>请求<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> http://freegeoip.net/json/?callback=handleResponse </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 这里指定的回调函数的名字叫做<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">handleResponse</code>。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">JSONP</code>是通过动态<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)"><script></code>元素来使用的,使用时可以为<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">src</code>属性指定一个跨域<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">URL</code>。</p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function handleResponse(response){ </div> <div class="line" style="height:20px"> alert("You’re at IP address " + response.ip + ", which is in " + </div> <div class="line" style="height:20px"> response.city + ", " + response.region_name); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> var script = document.createElement("script"); </div> <div class="line" style="height:20px"> script.src = "http://freegeoip.net/json/?callback=handleResponse"; </div> <div class="line" style="height:20px"> document.body.insertBefore(script, document.body.firstChild); </div></pre> </td> </tr> </tbody> </table> <h4 id="服务器发送事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 服务器发送事件</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>SSE</strong>支持短轮询、长轮训和HTTP流,而且能在断开连接时自动确定何时重新连接。</p> <h5 id="SSE-API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> SSE API</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 要预订新的事件流,首先要创建一个新的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">EventSource</code>对象,并传入一个入口点:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var source = new EventSource('myevents.php'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 传入的URL必须与创建对象的页面同源。<br> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">EventSource</code>的实例有一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readyState</code>属性:<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">0</code>表示正连接到服务器,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">1</code>表示打开了连接,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">2</code>表示关闭了连接。<br> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">EventSource</code>实例还有三个事件:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">open</code>:在建立连接时触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">message</code>:在从服务器接收到新事件时触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>:在无法建立连接时触发</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 服务器发回的数据以字符串形式保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event.data</code>中。<br> 默认情况下,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">EventSource</code>对象会保持与服务器的活动连接。如果想强制立即断开连接并且不在重新连接,可以调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">close()</code>方法。</p> <h4 id="Web-Sockets" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> Web Sockets</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 由于 Web Sockets 使用了自定义的协议,所以 URL 模式也略有不同。未加密的连接不再是 http:// ,而是 ws:// ;加密的连接也不是 https:// ,而是 wss:// 。</p> <h5 id="Web-Sockets-API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> Web Sockets API</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 创建一个WebSockets实例对象:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var socket = new WebSocket("ws://www.example.com/server.php"); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> WebSocket也有一个表示当前状态的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readyState</code>属性:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">WebSocket.OPENING (0)</code> :正在建立连接</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">WebSocket.OPEN (1)</code>:已经建立连接</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">WebSocket.CLOSING (2)</code>:正在关闭连接</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">WebSocket.CLOSE (3)</code>:已经关闭连接</li> </ul> <h5 id="发送和接收数据" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 发送和接收数据</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 向服务器发送数据,使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">send()</code>方法并传入任意字符串:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var socket = new WebSocket('ws:// www.example.com/server.php'); </div> <div class="line" style="height:20px"> socket.send('Hello World'); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> Web Sockets只能发送纯文本数据,对于复杂的数据结构,在发送之前,必须进行序列化。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var message = { </div> <div class="line" style="height:20px"> time: new Date(), </div> <div class="line" style="height:20px"> text: 'Hello world', </div> <div class="line" style="height:20px"> clientId: 'adfalsf39' </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> socket.send(JSON.stringify(message)); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 当服务器向客户端发来消息时,WebSocket对象就会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">message</code>事件。这个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">message</code>事件与其他传递消息的协议类似,也是把返回的数据保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event.data</code>属性中。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> socket.onmessage = function (event) { </div> <div class="line" style="height:20px"> var data = event.data; </div> <div class="line" style="height:20px"> // .... </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">send()</code>类似,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event.data</code>中返回的数据也是字符串,需要手工解析这些数据。</p> <h5 id="其他事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 其他事件</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> WebSocket对象还有其他三个事件,在连接生命周期的不同阶段触发:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">open</code>:在成功建立连接时触发</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>:在发生错误时触发,连接不能持续</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">close</code>:在连接关闭时触发</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> WebSocked对象不支持DOM 2级事件监听:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var socket = new WebSocket("ws://www.example.com/server.php"); </div> <div class="line" style="height:20px"> socket.onopen = function(){ </div> <div class="line" style="height:20px"> alert("Connection established."); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> socket.onerror = function(){ </div> <div class="line" style="height:20px"> alert("Connection error."); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> socket.onclose = function(){ </div> <div class="line" style="height:20px"> alert("Connection closed."); </div> <div class="line" style="height:20px"> }; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h2 id="高级技巧" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> 高级技巧</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="高级函数" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 高级函数</h3> <h4 id="安全的类型检测" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 安全的类型检测</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 用于区分原生和非原生<strong>JavaScript</strong>对象,通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Object.prototype.toString()</code>。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function isArray(value){ </div> <div class="line" style="height:20px"> return Object.prototype.toString.call(value) == "[object Array]"; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> function isFunction(value){ </div> <div class="line" style="height:20px"> return Object.prototype.toString.call(value) == "[object Function]"; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> function isRegExp(value){ </div> <div class="line" style="height:20px"> return Object.prototype.toString.call(value) == "[object RegExp]"; </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="作用域安全的构造函数" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 作用域安全的构造函数</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 防止构造函数内<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">this</code>指针的指向被改变(指向<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">window</code>)<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function Person (name, age, job) { </div> <div class="line" style="height:20px"> if (this instanceof Person) { </div> <div class="line" style="height:20px"> this.name = name; </div> <div class="line" style="height:20px"> this.age = age; </div> <div class="line" style="height:20px"> this.job = job; </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> return new Person(name, age, job); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="惰性载入函数" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 惰性载入函数</h4> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function createXHR(){ </div> <div class="line" style="height:20px"> if (typeof XMLHttpRequest != "undefined"){ </div> <div class="line" style="height:20px"> return new XMLHttpRequest(); </div> <div class="line" style="height:20px"> } else if (typeof ActiveXObject != "undefined"){ </div> <div class="line" style="height:20px"> if (typeof arguments.callee.activeXString != "string"){ </div> <div class="line" style="height:20px"> var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"], </div> <div class="line" style="height:20px"> i,len; </div> <div class="line" style="height:20px"> for (i=0,len=versions.length; i < len; i++){ </div> <div class="line" style="height:20px"> try { </div> <div class="line" style="height:20px"> new ActiveXObject(versions[i]); </div> <div class="line" style="height:20px"> arguments.callee.activeXString = versions[i]; </div> <div class="line" style="height:20px"> break; </div> <div class="line" style="height:20px"> } catch (ex){ </div> <div class="line" style="height:20px"> //跳过 </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> return new ActiveXObject(arguments.callee.activeXString); </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> throw new Error("No XHR object available."); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 第一种改法:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div> <div class="line" style="height:20px"> 23 </div> <div class="line" style="height:20px"> 24 </div> <div class="line" style="height:20px"> 25 </div> <div class="line" style="height:20px"> 26 </div> <div class="line" style="height:20px"> 27 </div> <div class="line" style="height:20px"> 28 </div> <div class="line" style="height:20px"> 29 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function createXHR () { </div> <div class="line" style="height:20px"> if (typeof XMLHttpRequest != 'undefined') { </div> <div class="line" style="height:20px"> createXHR = function () { </div> <div class="line" style="height:20px"> return new XMLHttpRequest(); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> } else if (typeof ActiveXObjext != 'undefined') { </div> <div class="line" style="height:20px"> createXHR = function () { </div> <div class="line" style="height:20px"> if (typeof arguments.callee.activeXString != 'string') { </div> <div class="line" style="height:20px"> var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"], </div> <div class="line" style="height:20px"> i,len; </div> <div class="line" style="height:20px"> for (i = 0; len = versions.length; i < len; i++) { </div> <div class="line" style="height:20px"> try { </div> <div class="line" style="height:20px"> new ActiveXObject(versions[i]); </div> <div class="line" style="height:20px"> arguments.callee.activeXString = versions[i]; </div> <div class="line" style="height:20px"> break; </div> <div class="line" style="height:20px"> } catch (e) { </div> <div class="line" style="height:20px"> // skip </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> return new ActiveXObject(arguments.callee.activeXString); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> createXHR = function () { </div> <div class="line" style="height:20px"> throw new Error('No XHR object available.'); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> return createXHR(); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 第二种改法:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div> <div class="line" style="height:20px"> 23 </div> <div class="line" style="height:20px"> 24 </div> <div class="line" style="height:20px"> 25 </div> <div class="line" style="height:20px"> 26 </div> <div class="line" style="height:20px"> 27 </div> <div class="line" style="height:20px"> 28 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var createXHR = (function () { </div> <div class="line" style="height:20px"> if (typeof XMLHttpRequest != 'undefined') { </div> <div class="line" style="height:20px"> return function () { </div> <div class="line" style="height:20px"> return new XMLHttpRequest(); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> } else if (typeof ActiveXObjext != 'undefined') { </div> <div class="line" style="height:20px"> return function () { </div> <div class="line" style="height:20px"> if (typeof arguments.callee.activeXString != 'string') { </div> <div class="line" style="height:20px"> var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"], </div> <div class="line" style="height:20px"> i,len; </div> <div class="line" style="height:20px"> for (i = 0; len = versions.length; i < len; i++) { </div> <div class="line" style="height:20px"> try { </div> <div class="line" style="height:20px"> new ActiveXObject(versions[i]); </div> <div class="line" style="height:20px"> arguments.callee.activeXString = versions[i]; </div> <div class="line" style="height:20px"> break; </div> <div class="line" style="height:20px"> } catch (e) { </div> <div class="line" style="height:20px"> // skip </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> return new ActiveXObject(arguments.callee.activeXString); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> return function () { </div> <div class="line" style="height:20px"> throw new Error('No XHR object available.'); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> })(); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="函数绑定" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 函数绑定</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">bind()</code>函数,语法如下:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> function bind (fn, context) { </div> <div class="line" style="height:20px"> return function () { </div> <div class="line" style="height:20px"> return fn.apply(context, arguments); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h2 id="离线应用与客户端存储" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> 离线应用与客户端存储</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="离线检测" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 离线检测</h3> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">navigator.onLine</code>属性可以判断设备否能访问网络。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> HTML5定义两个事件:<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">online</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">offline</code>,当网络状态变化时,分别触发这两个事件:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> EventUtil.addHandler(window, 'online', function () { </div> <div class="line" style="height:20px"> console.log('online'); </div> <div class="line" style="height:20px"> }); </div> <div class="line" style="height:20px"> EventUtil.addHandler(window, 'offline', function () { </div> <div class="line" style="height:20px"> console.log('offline'); </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h3 id="数据存储" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> 数据存储</h3> <h4 id="Web存储机制" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> Web存储机制</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> Web Storage规范包含两种对象的定义:<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">sessionStorage</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">globalStorage</code>。这两个对象在支持的浏览器中都是以<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">windows</code>对象属性的形式存在。</p> <h5 id="Storage类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Storage</code>类型</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Storage</code>类型提供最大的存储空间来存储名值对。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clear()</code>:删除所有值</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getItem(name)</code>:根据指定的名字<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">name</code>获取对应的值</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">key(index)</code>:获得<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">index</code>位置处的值的名字</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">removeItem(name)</code>:删除由<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">name</code>指定的名值对</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setItem(name, value)</code>:为指定的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">name</code>设置一个对应的值</li> </ul> <h5 id="sessionStorage对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">sessionStorage</code>对象</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">sessionStorage</code>对象存储特定于某个会话的数据,也就是该数据只保持到浏览器关闭。存储在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">sessionStorage</code>中的数据可以跨越页面刷新而存在。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> //使用方法存储数据 </div> <div class="line" style="height:20px"> sessionStorage.setItem("name", "Nicholas"); </div> <div class="line" style="height:20px"> //使用属性存储数据 </div> <div class="line" style="height:20px"> sessionStorage.book = "Professional JavaScript"; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //使用方法读取数据 </div> <div class="line" style="height:20px"> var name = sessionStorage.getItem("name"); </div> <div class="line" style="height:20px"> //使用属性读取数据 </div> <div class="line" style="height:20px"> var book = sessionStorage.book; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //使用delete 删除一个值——在WebKit 中无效 </div> <div class="line" style="height:20px"> delete sessionStorage.name; </div> <div class="line" style="height:20px"> //使用方法删除一个值 </div> <div class="line" style="height:20px"> sessionStorage.removeItem("book"); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 可以通过结合<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">length</code>属性和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">key()</code>方法来迭代<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">sessionStorage</code>中的值:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> for (var i = 0, len = sessionStorage.length; i < len; i++) { </div> <div class="line" style="height:20px"> var key = sessionStorage.key(i); </div> <div class="line" style="height:20px"> var value = sessionStorage.getItem(key); </div> <div class="line" style="height:20px"> console.log(key + ' = ' + value); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 还可以使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">for-in</code>循环来迭代<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">sessionStorage</code>中的值:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> for (var key in sessionStorage) { </div> <div class="line" style="height:20px"> var value = sessionStorage.getItem(key); </div> <div class="line" style="height:20px"> console.log(key + ' = ' + value); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="globalStorage对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">globalStorage</code>对象</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 这个对象的目的是跨越会话存储数据,,但有特定的访问限制。要使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">globalStorage</code>,首先要指定哪些域可以访问该数据。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> // 保存数据 </div> <div class="line" style="height:20px"> globalStorage['wrox.com'].name = 'Yeaseon'; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> // 获取数据 </div> <div class="line" style="height:20px"> var name = globalStorage['wrox.com'].name; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 上例,访问的是针对域名<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">wrox.com</code>的存储空间。<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">globalStorage</code>对象不是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Storage</code>的实例,<br> 而具体的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">globalStorage['wrox.com']</code>才是。这个存储空间对于<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">wrox.com</code>及其所有子域都是可以访问的。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> globalStorage["www.wrox.com"].name = "Yeaseon"; </div> <div class="line" style="height:20px"> globalStorage["www.wrox.com"].book = "Professional JavaScript"; </div> <div class="line" style="height:20px"> globalStorage["www.wrox.com"].removeItem("name"); </div> <div class="line" style="height:20px"> var book = globalStorage["www.wrox.com"].getItem("book"); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="localStorage对象" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">localStorage</code>对象</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">localStorage</code>对象是HTML5规范中作为持久保存客户端数据的方案,并且取代<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">globalStorage</code>。要访问同一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">localStorage</code>对象,页面必须来自同一个域名(子域名无效),必须同源。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> //使用方法存储数据 </div> <div class="line" style="height:20px"> localStorage.setItem("name", "Nicholas"); </div> <div class="line" style="height:20px"> //使用属性存储数据 </div> <div class="line" style="height:20px"> localStorage.book = "Professional JavaScript"; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //使用方法读取数据 </div> <div class="line" style="height:20px"> var name = localStorage.getItem("name"); </div> <div class="line" style="height:20px"> //使用属性读取数据 </div> <div class="line" style="height:20px"> var book = localStorage.book; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="storage事件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">storage</code>事件</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 对<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Storage</code>对象进行任何修改,都会在文档上触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">storage</code>事件。这个事件的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event</code>对象有以下属性。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">domain</code>:发生变化的存储空间的域名</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">key</code>:设置或删除的键名</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">newValue</code>:如果是设置值,则是新值;如果是删除键,则是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">null</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">oldValue</code>:键被更改之前的值</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在这四个属性中,IE8 和Firefox 只实现了<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">domain</code> 属性。在撰写本书的时候,WebKit 尚不支持<br> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">storage</code> 事件</p> <h4 id="IndexedDB" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> IndexedDB</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> Indexed Database API,简称<strong>IndexedDB</strong>,是在浏览器中保存结构化数据的一种数据库。<strong>IndexedDB</strong>设计的操作完全是异步进行。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var indexedDB = window.indexedDB || window.msIndexedDB || window.mozIndexedDB || window.webkitIndexedDB; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="数据库" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 数据库</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>IndexedDB</strong>就是一个数据库,它最大的特色就是使用对象保存数据,而不是使用表来保存数据。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">indexDB.open()</code>,传入一个数据库参数。如果该数据库存在就会发送一个打开它的请求;如果该数据库不存在,就会发送一个创建并打开它的请求。请求会返回一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBRequest</code>对象,这个对象上可以添加<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onerror</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onsuccess</code>事件处理程序。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var request, database; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> request = indexedDB.open('admin'); </div> <div class="line" style="height:20px"> request.onerror = function (event) { </div> <div class="line" style="height:20px"> console.log(event.target.errorCode); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> request.onsuccess = function (event) { </div> <div class="line" style="height:20px"> database = event.target.result; </div> <div class="line" style="height:20px"> }; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event.target</code>都指向<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">request</code>对象,因此他们可以互换使用。<br> 发生错误了,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event.target.errorCode</code>中将会保存一个错误码:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.UNKNOWN_ERR(1)</code>:意外错误</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.NON_TRANSIENT_ERR(2)</code>:操作不合法</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.NOT_FOUND_ERR(3)</code>:未发现要操作的数据库</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.CONSTRAINT_ERR(4)</code>:违反了数据库约束</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.DATA_ERR(5)</code>:提供给事务的数据不能满足要求</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.NOT_ALLOWED_ERR(6)</code>:操作不合法</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.TRANSACTION_INACTIVE_ERR(7)</code>:试图重用已完成的事务</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.ABORT_ERR(8)</code>:请求中断</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.READ_ONLY_ERR(9)</code>:试图在只读模式下写入或修改数据</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.TIMEOUT_ERR(10)</code>:在有效时间内未完成操作</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBDatebaseException.QUOTA_ERR(11)</code>:磁盘空间不足</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 指定数据库版本号,通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">setVersion()</code>方法:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> if (database.version != '1.0') { </div> <div class="line" style="height:20px"> request = database.setVersion('1.0'); </div> <div class="line" style="height:20px"> request.onerror = function (event) { </div> <div class="line" style="height:20px"> console.log(event.target.errorCode); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> request.onsuccess = function (event) { </div> <div class="line" style="height:20px"> console.log(''Database name: ' + database.name + ', Version: ' + database.version); </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> console.log(''Database name: ' + database.name + ', Version: ' + database.version); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="对象存储空间" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 对象存储空间</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 假设要保存的用户记录由用户名、密码等组成,那么保存一条记录的对象应该类似:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var user = { </div> <div class="line" style="height:20px"> username: '007', </div> <div class="line" style="height:20px"> firstname: 'James', </div> <div class="line" style="height:20px"> lastname: 'Bond', </div> <div class="line" style="height:20px"> password: 'foo' </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 如果使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">username</code>属性作为这个对象存储空间的键,这个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">username</code>必须全局唯一,而且大部分时候都要通过这个键来访问数据。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var store = db.createObjectStore('users', { keyPath: 'username' }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 其中第二个参数中的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">keyPath</code>属性,就是空间中将要保存的对象的一个属性,而这个属性将作为存储空间的键来使用。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 通过<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">add()</code>或<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">put()</code>方法来向存储空间添加数据。着两个方法都接收一个参数,就是要保存的对象。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> //users 中保存着一批用户对象 </div> <div class="line" style="height:20px"> var i=0, </div> <div class="line" style="height:20px"> request, </div> <div class="line" style="height:20px"> requests = [], </div> <div class="line" style="height:20px"> len = users.length; </div> <div class="line" style="height:20px"> while(i < len){ </div> <div class="line" style="height:20px"> request = store.add(users[i++]); </div> <div class="line" style="height:20px"> request.onerror = function(){ </div> <div class="line" style="height:20px"> //处理错误 </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> request.onsuccess = function(){ </div> <div class="line" style="height:20px"> //处理成功 </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> requests.push(request); </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h5 id="事务" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 事务</h5> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在数据库对象上调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">transaction()</code>可以创建事务,任何时候,只要想读取或修改数据,都要通过事务来组织所有操作。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> // 创建事务 </div> <div class="line" style="height:20px"> var transaction = db.transaction(); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 可以传入要访问的一或多个对象存储空间。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var transaction = db.transaction('users'); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> var transaction = db.transaction(['users', 'anotherStore']); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 前面这些事务都是以<strong>只读</strong>方式访问数据。要修改访问方式,必须在创建事务时传入第二个参数,这个参数表示访问模式:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBTransaction.READ_ONLY(0)</code>:只读</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBTransaction.READ_WRITE(1)</code>:读写</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">IDBTransaction.VERSION_CHANGE(2)</code>:改变</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 在Chrome中叫做<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">webkitIDBTransaction</code>,可以使用一下代码兼容:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 这样就能方便的指定<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">transaction()</code>第二个参数:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var transaction = db.transaction('users', IDBTransaction.READ_WRITE); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 取得事务的索引后,使用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">objectStore()</code>方法并传入存储空间的名称,就可以访问指定的存储空间。然后通过如下方法操作对象:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">add()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">put()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">get()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">delete()</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">clear()</code></li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var request = db.transaction('users').objectStore('users').get('007'); </div> <div class="line" style="height:20px"> request.onerror = function (event) { </div> <div class="line" style="height:20px"> console.log('Did not get the object!'); </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> request.onsuccess = function (event) { </div> <div class="line" style="height:20px"> var result = event.target.result; </div> <div class="line" style="height:20px"> console.log(result.firstName); // 'James' </div> <div class="line" style="height:20px"> }; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 也可以针对事务对象本身进行事件处理,存在两个事件<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onerror</code>,<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">oncomplete</code>:<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> transaction.onerror = function (event) { </div> <div class="line" style="height:20px"> // 整个事务都被取消了 </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> transaction.oncomplete = function (event) { </div> <div class="line" style="height:20px"> // 整个事务都成功完成了 </div> <div class="line" style="height:20px"> } </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <strong>注</strong>:在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">oncomplete</code>事件的事件对象中访问不到<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">get()</code>请求返回的数据,必须在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">onsuccess</code>事件中处理。</p> <h5 id="键范围" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; color:rgb(85,85,85); text-align:justify"> 键范围</h5> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange; </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 有四种定义键范围的方法:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">only()</code>:取得指定对象的键</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">lowerBound()</code>:指定结果集的下界</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">upperBound()</code>:指定结果集的上界</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">bound()</code>:同时指定上、下界</li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var onlyRange = IDBKeyRange.only("007"); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //从键为"007"的对象开始,然后可以移动到最后 </div> <div class="line" style="height:20px"> var lowerRange = IDBKeyRange.lowerBound("007"); </div> <div class="line" style="height:20px"> //从键为"007"的对象的下一个对象开始,然后可以移动到最后 </div> <div class="line" style="height:20px"> var lowerRange = IDBKeyRange.lowerBound("007", true); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //从头开始,到键为"ace"的对象为止 </div> <div class="line" style="height:20px"> var upperRange = IDBKeyRange.upperBound("ace"); </div> <div class="line" style="height:20px"> //从头开始,到键为"ace"的对象的上一个对象为止 </div> <div class="line" style="height:20px"> var upperRange = IDBKeyRange.upperBound("ace", true); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> //从键为"007"的对象开始,到键为"ace"的对象为止 </div> <div class="line" style="height:20px"> var boundRange = IDBKeyRange.bound("007", "ace"); </div> <div class="line" style="height:20px"> //从键为"007"的对象的下一个对象开始,到键为"ace"的对象为止 </div> <div class="line" style="height:20px"> var boundRange = IDBKeyRange.bound("007", "ace", true); </div> <div class="line" style="height:20px"> //从键为"007"的对象的下一个对象开始,到键为"ace"的对象的上一个对象为止 </div> <div class="line" style="height:20px"> var boundRange = IDBKeyRange.bound("007", "ace", true, true); </div> <div class="line" style="height:20px"> //从键为"007"的对象开始,到键为"ace"的对象的上一个对象为止 </div> <div class="line" style="height:20px"> var boundRange = IDBKeyRange.bound("007", "ace", false, true); </div></pre> </td> </tr> </tbody> </table> <h2 id="新型的API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:20px; color:rgb(85,85,85); text-align:justify"> 新型的API</h2> <hr style="height:3px; margin:40px 0px; border:none; background-color:rgb(221,221,221); color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <h3 id="Page-Visibility-API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> Page Visibility API</h3> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> Page Visibility API 是为了让开发人员知道页面是否对用户可见推出的。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.hidden</code>:表示页面是否隐藏的布尔值。</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">document.visibilityState</code> <ul style="list-style:none"> <li style="list-style:circle">页面在后台标签页中或浏览器最小化</li> <li style="list-style:circle">页面在前台标签页中</li> <li style="list-style:circle">实际的页面已经隐藏,但用户可以看到页面的预览</li> <li style="list-style:circle">页面在屏幕外执行预渲染处理</li> </ul> </li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">visibilitychange</code>事件:当文档可见性发生改变时,触发该事件。</li> </ul> <h3 id="Geolocation-API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> Geolocation API</h3> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> Geolocation API 在浏览器中的实现是<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">navigator.geolocation</code>对象。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">getCurrentPosition()</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 调用这个方法就会触发请求用户共享地理定位信息的对话框。这个方法接收三个参数:成功回调,可选的失败回调和可选的选项对象。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">成功回调</code>会接收到一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">Position</code>对象参数,该对象有两个属性:<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">coords</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">timestamp</code>。</p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">coords</code>对象中包含于位置相关的信息:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">latitude</code>:十进制度数表示的纬度</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">longitude</code>:十进制度数表示的经度</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">accuracy</code>:经纬度坐标的精度,以米为单位</li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> navigator.geolocation.getCurrentPosition(function (position) { </div> <div class="line" style="height:20px"> drawMapCenteredAt(position.coords.latitude, position.coords.longitude); </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">失败回调</code>在被调用的时候也会接受到一个参数,这个参数是一个对象,包含连个属性:<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">message</code>和<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">code</code>。<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">code</code>保存着一个数值,表示错误的类型:用户拒绝共享(1)、位置无效(2)或者超时(3)。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> navigator.geolocation.getCurrentPosition(function (position) { </div> <div class="line" style="height:20px"> drawMapCenteredAt(position.coords.latitude, position.coords.longitude); </div> <div class="line" style="height:20px"> }, function (error) { </div> <div class="line" style="height:20px"> console.log('Error code:' + error.code); </div> <div class="line" style="height:20px"> console.log('Error message:' + error.message); </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 第三个参数是一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">可选对象</code>,用于设定信息的类型。可以设置的选项有三个:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">enableHightAccuracy</code>:布尔值,表示必须尽可能使用最准确定的位置信息</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">timeout</code>:以毫秒数表示的等待位置信息的最长时间</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">maximumAge</code>:表示上一次取得的坐标信息的有效时间,以毫秒表示,如果时间到则重新取得新坐标信息</li> </ul> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> navigator.geolocation.getCurrentPosition(function (position) { </div> <div class="line" style="height:20px"> drawMapCenteredAt(position.coords.latitude, position.coords.longitude); </div> <div class="line" style="height:20px"> }, function (error) { </div> <div class="line" style="height:20px"> console.log('Error code:' + error.code); </div> <div class="line" style="height:20px"> console.log('Error message:' + error.message); </div> <div class="line" style="height:20px"> }, { </div> <div class="line" style="height:20px"> enableHighAccuracy: true, </div> <div class="line" style="height:20px"> timeout: 5000, </div> <div class="line" style="height:20px"> maximumAge: 25000 </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <h3 id="File-API" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:18px; color:rgb(85,85,85); text-align:justify"> File API</h3> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> File API 在表单中的文件输入字段的基础上,又添加了一些直接访问文件信息的接口。HTML5在DOM中为文件输入元素添加了一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">files</code>集合。每个<strong>File</strong>对象都有下列只读属性。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">name</code>:本地文件系统的文件名</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">size</code>:文件的字节大小</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">type</code>:字符串,文件的MIME类型</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">lastModifiedDate</code>:字符串,文件上一次修改的时间</li> </ul> <h4 id="FileReader-类型" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> FileReader 类型</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> FileReader 类型实现的是一种异步文件读取机制。可以把<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FileReader</code>想象成<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XMLHttpRequest</code>。</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readAsText(file, encoding)</code>:以纯文本形式读取文件,将读取到的文本保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">result</code>属性中,第二个参数用于指定编码类型(可选)</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readAsDataURL(file)</code>:读取文件并将文件以数据URI形式保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">result</code>属性中</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readAsBinaryString(file)</code>:读取文件并将一个字符串保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">result</code>属性中,字符串中的每个字符表示一字节</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">readAsArrayBuffer(file)</code>:读取文件并将一个包含文件内容的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">ArrayBuffer</code>保存在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">result</code>属性中</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 由于读取过程是异步的,所以<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FileReader</code>提供了三个事件:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">progress</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">load</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">progress</code>事件,每50ms就会触发一次,通过事件对象可以获得与<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">progress</code>事件相同的信息:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">lengthComputable</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">loaded</code></li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">total</code></li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 由于种种原因无法读取文件,都会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>事件,相关信息都会保存到<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FileReader</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error</code>属性中。<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">error.code</code>即错误码:</p> <ul style="list-style:none; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">1</code>:为找到文件</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">2</code>:安全性错误</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">3</code>:读取中断</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">4</code>:文件不可读</li> <li style="list-style:circle"><code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">5</code>:编码错误</li> </ul> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 文件加载成功后会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">load</code>事件。</p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div> <div class="line" style="height:20px"> 23 </div> <div class="line" style="height:20px"> 24 </div> <div class="line" style="height:20px"> 25 </div> <div class="line" style="height:20px"> 26 </div> <div class="line" style="height:20px"> 27 </div> <div class="line" style="height:20px"> 28 </div> <div class="line" style="height:20px"> 29 </div> <div class="line" style="height:20px"> 30 </div> <div class="line" style="height:20px"> 31 </div> <div class="line" style="height:20px"> 32 </div> <div class="line" style="height:20px"> 33 </div> <div class="line" style="height:20px"> 34 </div> <div class="line" style="height:20px"> 35 </div> <div class="line" style="height:20px"> 36 </div> <div class="line" style="height:20px"> 37 </div> <div class="line" style="height:20px"> 38 </div> <div class="line" style="height:20px"> 39 </div> <div class="line" style="height:20px"> 40 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var filesList = document.getElementById('files-list'); </div> <div class="line" style="height:20px"> EventUtil.addHandler(filesList, 'change', function (event) { </div> <div class="line" style="height:20px"> var info = '', </div> <div class="line" style="height:20px"> output = document.getElementById('output'), </div> <div class="line" style="height:20px"> progress = document.getElementById('progress'), </div> <div class="line" style="height:20px"> files = EventUtil.getTarget(event).files, </div> <div class="line" style="height:20px"> type = 'default', </div> <div class="line" style="height:20px"> reader = new FileReader(); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> if (/image/.test(files[0].type)) { </div> <div class="line" style="height:20px"> reader.readAsDateURL(files[0]); </div> <div class="line" style="height:20px"> type = 'image'; </div> <div class="line" style="height:20px"> } else { </div> <div class="line" style="height:20px"> reader.readAsText(files[0]); </div> <div class="line" style="height:20px"> type = 'text'; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> reader.onerror = function () { </div> <div class="line" style="height:20px"> output.innerHTML = 'Could not read file, error code is ' + reader.error.code; </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> reader.onprogress = function () { </div> <div class="line" style="height:20px"> if (event.lengthComputable) { </div> <div class="line" style="height:20px"> progress.innerHTML = event.loaded + '/' + event.total; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> reader.onload = function () { </div> <div class="line" style="height:20px"> var html = ''; </div> <div class="line" style="height:20px"> switch (type) { </div> <div class="line" style="height:20px"> case 'image': </div> <div class="line" style="height:20px"> html = '<img src=\"' + reader.result + '\">'; </div> <div class="line" style="height:20px"> break; </div> <div class="line" style="height:20px"> case 'text': </div> <div class="line" style="height:20px"> html = reader.result; </div> <div class="line" style="height:20px"> break; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> output.innerHTML = html; </div> <div class="line" style="height:20px"> }; </div> <div class="line" style="height:20px"> }); </div></pre> </td> </tr> </tbody> </table> <h4 id="读取拖放的文件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 读取拖放的文件</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 从桌面上把文件拖放到浏览器中会触发<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">drop</code> 事件。而且可以在<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">event.dataTransfer. files</code>中读取到被放置的文件,当然此时它是一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">File</code> 对象,与通过文件输入字段取得的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">File</code> 对象一样。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div> <div class="line" style="height:20px"> 23 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var droptarget = document.getElementById('droptarget'); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> function handleEvent(event) { </div> <div class="line" style="height:20px"> var info = '', </div> <div class="line" style="height:20px"> output = document.getElementById('output'); </div> <div class="line" style="height:20px"> files, i, len; </div> <div class="line" style="height:20px"> EventUtil.preventDefault(event); </div> <div class="line" style="height:20px"> if (event.type == 'drop') { </div> <div class="line" style="height:20px"> files = event.dataTransfer.files; //转换成File对象 </div> <div class="line" style="height:20px"> i = 0; </div> <div class="line" style="height:20px"> len = files.length; </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> while (i < len) { </div> <div class="line" style="height:20px"> info += files[i].name + ' (' + files[i].type + ', ' + files[i].size + ' bytes)<br>'; </div> <div class="line" style="height:20px"> i++; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> output.innerHTML = info; </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> } </div> <div class="line" style="height:20px"> // 阻止默认事件,只有 drop 事件会被处理 </div> <div class="line" style="height:20px"> EventUtil.addHandler(droptarget, "dragenter", handleEvent); </div> <div class="line" style="height:20px"> EventUtil.addHandler(droptarget, "dragover", handleEvent); </div> <div class="line" style="height:20px"> EventUtil.addHandler(droptarget, "drop", handleEvent); </div></pre> </td> </tr> </tbody> </table> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> </p> <h4 id="使用XHR上传文件" style="margin:20px 0px 15px; padding:10px 0px 0px; line-height:1.5; font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:16px; color:rgb(85,85,85); text-align:justify"> 使用XHR上传文件</h4> <p style="margin-top:0px; margin-bottom:25px; color:rgb(85,85,85); font-family:Lato,"PingFang SC","Microsoft YaHei",sans-serif; font-size:14px; text-align:justify"> 创建一个<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FormDate</code>对象,通过它调用<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">append()</code>方法并传入相应的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">File</code>对象作为参数,再把<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">FormData</code>对象传递给<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">XHR</code>的<code style="font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; padding:2px 4px; word-wrap:break-word; color:rgb(77,77,76); background:rgb(247,247,247)">send()</code>方法。<br> </p> <table style="border-collapse:collapse; border-spacing:0px; margin:0px; width:auto; border:none; font-size:14px; table-layout:fixed"> <tbody> <tr style="background-color:rgb(249,249,249)"> <td class="gutter" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(134,145,148); background:rgb(239,242,243); line-height:1.6; border:none; text-align:right"> <div class="line" style="height:20px"> 1 </div> <div class="line" style="height:20px"> 2 </div> <div class="line" style="height:20px"> 3 </div> <div class="line" style="height:20px"> 4 </div> <div class="line" style="height:20px"> 5 </div> <div class="line" style="height:20px"> 6 </div> <div class="line" style="height:20px"> 7 </div> <div class="line" style="height:20px"> 8 </div> <div class="line" style="height:20px"> 9 </div> <div class="line" style="height:20px"> 10 </div> <div class="line" style="height:20px"> 11 </div> <div class="line" style="height:20px"> 12 </div> <div class="line" style="height:20px"> 13 </div> <div class="line" style="height:20px"> 14 </div> <div class="line" style="height:20px"> 15 </div> <div class="line" style="height:20px"> 16 </div> <div class="line" style="height:20px"> 17 </div> <div class="line" style="height:20px"> 18 </div> <div class="line" style="height:20px"> 19 </div> <div class="line" style="height:20px"> 20 </div> <div class="line" style="height:20px"> 21 </div> <div class="line" style="height:20px"> 22 </div> <div class="line" style="height:20px"> 23 </div> <div class="line" style="height:20px"> 24 </div> <div class="line" style="height:20px"> 25 </div> <div class="line" style="height:20px"> 26 </div> <div class="line" style="height:20px"> 27 </div> <div class="line" style="height:20px"> 28 </div> <div class="line" style="height:20px"> 29 </div> <div class="line" style="height:20px"> 30 </div> <div class="line" style="height:20px"> 31 </div> <div class="line" style="height:20px"> 32 </div> <div class="line" style="height:20px"> 33 </div> <div class="line" style="height:20px"> 34 </div></pre> </td> <td class="code" style="padding:0px; vertical-align:middle; border:none"> <pre style="overflow:auto; font-family:consolas,Menlo,"PingFang SC","Microsoft YaHei",monospace; font-size:13px; margin-top:0px; margin-bottom:0px; padding:10px; color:rgb(77,77,76); background:rgb(247,247,247); line-height:1.6; border:none"> <div class="line" style="height:20px"> var droptarget = document.getElementById('droptarget'); </div> <div class="line" style="height:20px"></div> <div class="line" style="height:20px"> function handleEvent(event) { </div> <div class="line" style="height:20px"> var info = '', </div> <div class="line" style="height:20px"> output = document.getElementById('output'), </div></pre></td> </tr> </tbody> </table> </div> </div> </div> </div> </div> <!--PC和WAP自适应版--> <div id="SOHUCS" sid="1276346245878661120"></div> <script type="text/javascript" src="/views/front/js/chanyan.js"></script> <!-- 文章页-底部 动态广告位 --> <div class="youdao-fixed-ad" id="detail_ad_bottom"></div> </div> <div class="col-md-3"> <div class="row" id="ad"> <!-- 文章页-右侧1 动态广告位 --> <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_1"> </div> </div> <!-- 文章页-右侧2 动态广告位 --> <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_2"></div> </div> <!-- 文章页-右侧3 动态广告位 --> <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad"> <div class="youdao-fixed-ad" id="detail_ad_3"></div> </div> </div> </div> </div> </div> </div> <div class="container"> <h4 class="pt20 mb15 mt0 border-top">你可能感兴趣的:(【Javascript点滴知识,】)</h4> <div id="paradigm-article-related"> <div class="recommend-post mb30"> <ul class="widget-links"> <li><a href="/article/1903015207043657728.htm" title="前端面试:[React] Recoil 里面 selector 支持哪些参数?" target="_blank">前端面试:[React] Recoil 里面 selector 支持哪些参数?</a> <span class="text-muted">returnShitBoy</span> <a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/%E9%9D%A2%E8%AF%95/1.htm">面试</a><a class="tag" taget="_blank" href="/search/react.js/1.htm">react.js</a> <div>在Recoil中,selector用于派生状态并可以通过传递不同的参数来实现更强大的功能。创建selector时,可以传入以下参数:1.key类型:string描述:是selector的唯一标识符。每个selector和atom都必须有一个唯一的key,否则会抛出错误。这个key用于在Recoil状态树中进行识别。示例:javascriptconstmySelector=selector({key</div> </li> <li><a href="/article/1903006636931346432.htm" title="计算机二级c语言知识点6" target="_blank">计算机二级c语言知识点6</a> <span class="text-muted">xu_hhh_</span> <a class="tag" taget="_blank" href="/search/%E8%AE%A1%E7%AE%97%E6%9C%BA%E4%BA%8C%E7%BA%A7c%E8%AF%AD%E8%A8%80%E9%80%89%E6%8B%A9%E9%A2%98/1.htm">计算机二级c语言选择题</a><a class="tag" taget="_blank" href="/search/c%E8%AF%AD%E8%A8%80/1.htm">c语言</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>函数形参的值,不会改变对应实参的值函数可以返回地址值&x不可以给指针变量赋一个整数作为地址值当在程序的开头包含头文件stdio.h时,可以给指针变量赋NULLfun(char*a,char*b){while((*b=*a)!=‘\0’){a++;b++;}}这个函数实现的功能是将a所指的字符串赋给b所指的空间,此函数也会将\0赋给b,因为括号里的表达式(*b=*a)先执行,后判断是否=\0若有定义</div> </li> <li><a href="/article/1903004853030940672.htm" title="使用Three.js渲染器创建炫酷3D场景" target="_blank">使用Three.js渲染器创建炫酷3D场景</a> <span class="text-muted"></span> <div>引言在当今数字化的时代,3D图形技术正以其独特的魅力在各个领域掀起波澜。从影视制作到游戏开发,从虚拟现实到网页交互,3D场景以其强烈的视觉冲击力和沉浸式的体验,成为了吸引用户、传达信息的重要手段。而Three.js,作为一款功能强大且广受欢迎的JavaScript3D库,为我们提供了便捷、高效的途径来创建令人炫目的3D场景。本文将深入探讨使用Three.js渲染器创建炫酷3D场景的方方面面,带领读</div> </li> <li><a href="/article/1902997655034851328.htm" title="用Babel操作AST实现JavaScript代码的自动化生成与转换" target="_blank">用Babel操作AST实现JavaScript代码的自动化生成与转换</a> <span class="text-muted"></span> <div>目录目录环境搭建代码:修改AST的逻辑重命名函数名重命名变量并修改变量值函数调用替换控制流扁平化还原删除未使用的变量对象属性简化条件表达式优化表达式还原环境搭建安装环境npminstall@babel/parser@babel/traverse@babel/generator@babel/typesast转换的代码框架constfs=require('fs');constvm=require('n</div> </li> <li><a href="/article/1902983683464294400.htm" title="HAL库操作STM32串口" target="_blank">HAL库操作STM32串口</a> <span class="text-muted">2021.09</span> <a class="tag" taget="_blank" href="/search/STM32-CubeMX/1.htm">STM32-CubeMX</a><a class="tag" taget="_blank" href="/search/stm32/1.htm">stm32</a><a class="tag" taget="_blank" href="/search/%E5%8D%95%E7%89%87%E6%9C%BA/1.htm">单片机</a><a class="tag" taget="_blank" href="/search/arm/1.htm">arm</a> <div>本次博客知识来自于韦东山老师的7天物联网课程。一、cubeMX产生工程框架先从左侧选择串口1,再选择异步通信。二、分析程序如下图,cubeMX自动生成了串口初始化函数。三、编写程序以上初始化完成后,就可以使用HAL库提供的“HAL_UART_Transmit()”从串口发送数据,使用“HAL_UART_Receive()”接收数据,但这样使用不方便,需要自己处理数据类型。在学习C语言时,通常使用p</div> </li> <li><a href="/article/1902980407872253952.htm" title="10分钟了解基金基础知识" target="_blank">10分钟了解基金基础知识</a> <span class="text-muted">leo_厉锵</span> <a class="tag" taget="_blank" href="/search/%E9%87%91%E8%9E%8D%E6%A0%8F/1.htm">金融栏</a><a class="tag" taget="_blank" href="/search/%E9%87%91%E8%9E%8D/1.htm">金融</a> <div>一、基金的本质股票、债券和基金具有一定的可比性,而银行理财产品较为特殊。(一)股票股票代表一个公司的股份。拥有公司股票就相当于拥有部分公司股份。股票投资收益潜力大,因为公司可能是赚钱机器从而导致股价暴涨;但风险也很高,因为公司可能经营不善致使股价暴跌。(二)债券债券代表一种债权,即借钱给别人。例如国债,可理解为国家向你借钱并打借条,约定偿还时间和利息。债券代表着债券关系。(三)基金股票和债券属于直</div> </li> <li><a href="/article/1902980407121473536.htm" title="10分钟了解股市基础知识" target="_blank">10分钟了解股市基础知识</a> <span class="text-muted">leo_厉锵</span> <a class="tag" taget="_blank" href="/search/%E9%87%91%E8%9E%8D%E6%A0%8F/1.htm">金融栏</a><a class="tag" taget="_blank" href="/search/%E5%A4%A7%E6%95%B0%E6%8D%AE/1.htm">大数据</a> <div>一、交易时间周一至周五,法定节假日除外。盘前集合竞价(9:15-9:25),25分开出开盘价。其中15-20分可挂单可撤单,20-25分可挂单不可撤单。盘中连续交易时间为上午9:30-11:30,下午13:00-14:57。尾盘集合竞价时间为14:57-15:00,三分钟可挂单不可撤单,15:00开出收盘价。创业板和科创板在15:05-15:30有盘后交易时间,沪深两市主板无。夜市委托一般为晚上1</div> </li> <li><a href="/article/1902966659358978048.htm" title="【布鲁姆6大认知层级】" target="_blank">【布鲁姆6大认知层级】</a> <span class="text-muted">搞技术的季</span> <a class="tag" taget="_blank" href="/search/%E7%BB%8F%E9%AA%8C%E5%88%86%E4%BA%AB/1.htm">经验分享</a> <div>认知思维目标层次由低到高、由简到繁分为六个层次,层层递进,这6个层级分别是:记忆——理解——应用——分析——评价——创新。第一层:记忆是指认识并记忆概念、知识,将其储存在大脑并及时提取,例如背单词、古诗、名词概念等。这一层次所涉及的是具体知识或抽象知识的辨认,虽然机械,但对学习和解决更复杂的问题来说是必不可少的基础环节。第二层:理解是指对事物或知识的领会,当学习者对"新"知识与原有知识产生联系时,</div> </li> <li><a href="/article/1902966659841323008.htm" title="【知识管理】" target="_blank">【知识管理】</a> <span class="text-muted">搞技术的季</span> <a class="tag" taget="_blank" href="/search/%E7%BB%8F%E9%AA%8C%E5%88%86%E4%BA%AB/1.htm">经验分享</a> <div>一、概念:知识:公司内部的个体的、部门的、企业的,甚至是行业的不同的知识层次所综合组成的知识网络,包括品牌市场、渠道、知识产权、技术标准、技术秘密、产品附加值、竞争情报等多种内容。知识管理:是传递知识的过程,由于传递人员的经验问题或者知识源的问题,可能导致较大的知识损耗。而知识管理的过程通过对隐性知识的挖掘和一系列知识管理的活动,可以有效提升知识转化的效率,体现企业内知识资产的价值。二、要素和现状</div> </li> <li><a href="/article/1902959975316123648.htm" title="yum install locate出现Error: Unable to find match: locate解决方案" target="_blank">yum install locate出现Error: Unable to find match: locate解决方案</a> <span class="text-muted">爱编程的喵喵</span> <a class="tag" taget="_blank" href="/search/Linux%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/1.htm">Linux解决方案</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/locate/1.htm">locate</a><a class="tag" taget="_blank" href="/search/yum/1.htm">yum</a><a class="tag" taget="_blank" href="/search/%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/1.htm">解决方案</a> <div>  大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作的方式对所学的知识进行总结与归纳,不仅形成深入且独到的理解,而且能够帮助新手快速入门。  本文主要介绍了yuminstalllocate出现</div> </li> <li><a href="/article/1902953037056503808.htm" title="一、大语言模型微调 vs. 大语言模型应用" target="_blank">一、大语言模型微调 vs. 大语言模型应用</a> <span class="text-muted">AI Echoes</span> <a class="tag" taget="_blank" href="/search/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/1.htm">深度学习</a><a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/1.htm">人工智能</a><a class="tag" taget="_blank" href="/search/deepseek/1.htm">deepseek</a><a class="tag" taget="_blank" href="/search/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/1.htm">机器学习</a><a class="tag" taget="_blank" href="/search/%E7%AE%97%E6%B3%95/1.htm">算法</a> <div>一、大语言模型微调vs.大语言模型应用1.微调(Fine-Tuning)的含义与特点定义与作用微调指在预训练好(通用)的基础模型上,通过在特定领域或任务的数据集上进一步训练来调整模型参数,使其在该领域任务中获得更优表现。这种方法可以使通用模型“定制化”,更好地理解专业术语和领域知识,从而提升准确性和响应质量。例如,为医疗、法律、金融等垂直领域构建专属模型,往往需要在预训练模型基础上进行微调。特点参</div> </li> <li><a href="/article/1902948746782633984.htm" title="LeetCode热题100JS(59/100)第十一天|46|78|17|39|22" target="_blank">LeetCode热题100JS(59/100)第十一天|46|78|17|39|22</a> <span class="text-muted">Alicesflower</span> <a class="tag" taget="_blank" href="/search/LeetCode%E7%83%AD%E9%A2%98100JS/1.htm">LeetCode热题100JS</a><a class="tag" taget="_blank" href="/search/leetcode/1.htm">leetcode</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/%E7%AE%97%E6%B3%95/1.htm">算法</a> <div>46.全排列题目链接:46.全排列难度:中等刷题状态:2刷新知识:解题过程思考示例1:输入:nums=[1,2,3]输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]题解分析参考题解链接:全排列放下1刷过程/***@param{number[]}nums*@return{number[][]}*///varpermute=function(num</div> </li> <li><a href="/article/1902948744316383232.htm" title="一些经纬度知识" target="_blank">一些经纬度知识</a> <span class="text-muted">AWen_X</span> <a class="tag" taget="_blank" href="/search/Java/1.htm">Java</a><a class="tag" taget="_blank" href="/search/%E5%AE%9A%E4%BD%8D/1.htm">定位</a><a class="tag" taget="_blank" href="/search/%E7%89%A9%E8%81%94%E7%BD%91/1.htm">物联网</a><a class="tag" taget="_blank" href="/search/java/1.htm">java</a> <div>1、横纬竖经2、lng经度:-180~180,东经正数,西经负数3、lat纬度:-90~90,北纬正数,南纬负数4、经纬度1度=60分=3600秒5、地球的子午线总长度大约40008km。纬度1度=大约111km纬度1分=大约1.85km纬度1秒=大约30.8m6、中国的经纬度范围大约为:纬度3.86~53.55,经度73.66~135.057、越北面的地方纬度数值越大,越东面的地方经度数值越大N</div> </li> <li><a href="/article/1902948491613761536.htm" title="知识图谱在人工智能语义理解与推理中的关键作用及发展研究" target="_blank">知识图谱在人工智能语义理解与推理中的关键作用及发展研究</a> <span class="text-muted">@王威&</span> <a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/1.htm">人工智能</a> <div>摘要本文聚焦知识图谱,深入剖析其在人工智能语义理解与推理中的核心作用。阐述知识图谱的构建原理、表示方法,分析其在自然语言处理、智能问答系统、推荐系统等多领域助力语义理解与推理的应用,探讨面临的挑战并展望未来发展方向,全面呈现知识图谱对人工智能发展的重要价值与深远影响。一、引言在人工智能追求更精准理解和处理人类语言与知识的进程中,知识图谱成为关键技术。它以结构化形式组织海量知识,揭示实体间复杂关系,</div> </li> <li><a href="/article/1902935229820104704.htm" title="如何用大模型评估大模型——PAI-Judge裁判员大语言模型的实现简介" target="_blank">如何用大模型评估大模型——PAI-Judge裁判员大语言模型的实现简介</a> <span class="text-muted"></span> <a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0%E5%A4%A7%E6%A8%A1%E5%9E%8Bllm/1.htm">人工智能机器学习大模型llm</a> <div>背景:为什么需要一个「裁判员大语言模型」?随着大模型(LLM)技术的爆发式应用,如何快速、客观评估模型回复质量成为行业痛点。对于回答客观问题的LLM,目前业内已经有比较成熟的数据集进行效果评测与模型打榜。但是如何对一个开放式生成LLM进行效果评估,尤其在知识问答、客服对话、内容合规、RAG(检索增强生成)等场景中,目前主流的评测方式仍存在一定的局限性:人工标注:成本高昂、效率低下;传统的自动化评估</div> </li> <li><a href="/article/1902929683826143232.htm" title="Node.js 格式化时间的两种方法:原生 Date 与 npm 包 moment 详解" target="_blank">Node.js 格式化时间的两种方法:原生 Date 与 npm 包 moment 详解</a> <span class="text-muted">还是鼠鼠</span> <a class="tag" taget="_blank" href="/search/node.js/1.htm">node.js</a><a class="tag" taget="_blank" href="/search/node.js/1.htm">node.js</a><a class="tag" taget="_blank" href="/search/npm/1.htm">npm</a><a class="tag" taget="_blank" href="/search/%E5%89%8D%E7%AB%AF/1.htm">前端</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/vscode/1.htm">vscode</a> <div>目录Node.js格式化时间的两种做法:内置方法与npm包1.使用JavaScript内置方法格式化时间示例:使用Date对象格式化时间运行程序示例输出原理解析2.使用npm包moment进行时间格式化安装moment示例:使用moment格式化时间运行程序示例输出原理解析3.两种方法的对比4.结论在Node.js开发中,格式化时间是一个常见的需求。例如,将时间格式化为YYYY-MM-DDHH:m</div> </li> <li><a href="/article/1902924517844512768.htm" title="【WinPcap】——ARP欺骗" target="_blank">【WinPcap】——ARP欺骗</a> <span class="text-muted">猫和鱼爪</span> <a class="tag" taget="_blank" href="/search/WinPcap/1.htm">WinPcap</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C%E5%8D%8F%E8%AE%AE/1.htm">网络协议</a><a class="tag" taget="_blank" href="/search/winpcap/1.htm">winpcap</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C%E5%8D%8F%E8%AE%AE/1.htm">网络协议</a> <div>利用WinPcap的简单ARP欺骗基础知识关于WinPcap在vc的环境搭建关于ARP等结构下图是从TCP/IP详解中摘录的图片:</div> </li> <li><a href="/article/1902924264558882816.htm" title="【C++】C++从入门到精通教程(持续更新...)" target="_blank">【C++】C++从入门到精通教程(持续更新...)</a> <span class="text-muted">废人一枚</span> <a class="tag" taget="_blank" href="/search/C%2B%2B/1.htm">C++</a><a class="tag" taget="_blank" href="/search/c%2B%2B/1.htm">c++</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>前言最近在整理之前一些C++资料,重新整理出了一套C++从基础到实践的教程,包含概念、代码、运行结果以及知识点的扩展,感兴趣的后续大家持续关注。以下是更新的文章目录,文章之后整理了一个知识思维导图,看起来比较清楚点。目录1、C++基础知识C++基础知识一个简单的C++程序函数重载引用的概念引用与指针的区别引用作为函数参数引用作为返回值面向对象类的定义类的声明结构体与类的区别inline函数this</div> </li> <li><a href="/article/1902918714932654080.htm" title="vr中的计算机知识,VR技术基本常识" target="_blank">vr中的计算机知识,VR技术基本常识</a> <span class="text-muted">淡庸</span> <a class="tag" taget="_blank" href="/search/vr%E4%B8%AD%E7%9A%84%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9F%A5%E8%AF%86/1.htm">vr中的计算机知识</a> <div>虚拟现实技术是仿真技术的一个重要方向是仿真技术与计算机图形学人机接口技术多媒体技术传感技术网络技术等多种技术的集合是一门富有挑战性的交叉技术前沿学科和研究领域。虚拟现实技术(VR)主要包括模拟环境、感知、自然技能和传感设备等方面。模拟环境是由计算机生成的、实时动态的三维立体逼真图像。感知是指理想的VR应该具有一切人所具有的感知。除计算机图形技术所生成的视觉感知外,还有听觉、触觉、力觉、运动等感知,</div> </li> <li><a href="/article/1902913032594452480.htm" title="YOLOv12优化:图像去噪 | AAAI2025 Transformer |一种基于Transformer的盲点网络(TBSN)架构 ,结合空间和通道自注意力层来增强网络能力" target="_blank">YOLOv12优化:图像去噪 | AAAI2025 Transformer |一种基于Transformer的盲点网络(TBSN)架构 ,结合空间和通道自注意力层来增强网络能力</a> <span class="text-muted">AI小怪兽</span> <a class="tag" taget="_blank" href="/search/YOLOv12%E9%AD%94%E6%9C%AF%E5%B8%88/1.htm">YOLOv12魔术师</a><a class="tag" taget="_blank" href="/search/YOLO/1.htm">YOLO</a><a class="tag" taget="_blank" href="/search/transformer/1.htm">transformer</a><a class="tag" taget="_blank" href="/search/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/1.htm">深度学习</a><a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/1.htm">人工智能</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>提出了一种基于Transformer的盲点网络(TBSN)架构,通过分析和重新设计Transformer运算符以满足盲点要求。TBSN遵循扩张BSN的架构原则,并结合空间和通道自注意力层来增强网络能力。如何使用:1)结合C3k2二次创新使用;2)结合A2C2f二次创新使用;亮点包括:1.提出了一种新的基于Transformer的盲点网络(TBSN)架构;2.引入了知识蒸馏策略来提高计算效率;3.在</div> </li> <li><a href="/article/1902910387926396928.htm" title="JavaScript 中的性能优化:从基础到高级技巧" target="_blank">JavaScript 中的性能优化:从基础到高级技巧</a> <span class="text-muted">lina_mua</span> <a class="tag" taget="_blank" href="/search/%E6%B7%B1%E5%85%A5/1.htm">深入</a><a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/1.htm">性能优化</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a> <div>1.引言1.1性能优化的重要性在现代前端开发中,性能优化是提升用户体验的关键。无论是页面加载速度、交互响应时间,还是内存占用,性能优化都能显著提升应用的流畅度和用户满意度。1.2本文的目标本文旨在深入探讨JavaScript中的性能优化,从基础到高级技巧,帮助开发者理解性能优化的核心概念,并掌握其在实际开发中的应用。2.性能优化的基础2.1什么是性能优化?性能优化是指通过改进代码、减少资源消耗、优</div> </li> <li><a href="/article/1902910260121759744.htm" title="数智读书笔记系列021《大数据医疗》:探索医疗行业的智能变革" target="_blank">数智读书笔记系列021《大数据医疗》:探索医疗行业的智能变革</a> <span class="text-muted">Allen_Lyb</span> <a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%99%BA%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0/1.htm">数智读书笔记</a><a class="tag" taget="_blank" href="/search/%E5%A4%A7%E6%95%B0%E6%8D%AE/1.htm">大数据</a><a class="tag" taget="_blank" href="/search/%E5%81%A5%E5%BA%B7%E5%8C%BB%E7%96%97/1.htm">健康医疗</a><a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/1.htm">人工智能</a><a class="tag" taget="_blank" href="/search/python/1.htm">python</a> <div>一、书籍介绍《大数据医疗》由徐曼、沈江、余海燕合著,由机械工业出版社出版。徐曼是南开大学商学院副教授,在大数据驱动的智能决策研究领域颇有建树,尤其在大数据驱动的医疗与健康决策方面有着深入研究,曾获天津优秀博士论文、教育部博士研究生新人奖。沈江等作者也在相关学术和实践领域有着丰富的经验和深厚的专业知识。这本书系统且深入地探讨了大数据技术在医疗领域的应用与变革,对推动医疗行业的智能化发展具有重要的理论</div> </li> <li><a href="/article/1902910005783359488.htm" title="Visual C++从入门到精通第三版 PDF 下载" target="_blank">Visual C++从入门到精通第三版 PDF 下载</a> <span class="text-muted">范武心Lucinda</span> <div>VisualC++从入门到精通第三版PDF下载【下载地址】VisualC从入门到精通第三版PDF下载VisualC++从入门到精通第三版PDF下载项目地址:https://gitcode.com/open-source-toolkit/f4bb4资源介绍本仓库提供《VisualC++从入门到精通第三版》的PDF版本下载。这本书是一本非常适合初学者的入门书籍,内容涵盖了从C++基础知识到Visual</div> </li> <li><a href="/article/1902904837750714368.htm" title="【AI大模型应用开发】RAG-Fusion框架:忘掉 RAG,未来是 RAG-Fusion" target="_blank">【AI大模型应用开发】RAG-Fusion框架:忘掉 RAG,未来是 RAG-Fusion</a> <span class="text-muted">同学小张</span> <a class="tag" taget="_blank" href="/search/%E5%A4%A7%E6%A8%A1%E5%9E%8B/1.htm">大模型</a><a class="tag" taget="_blank" href="/search/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/1.htm">人工智能</a><a class="tag" taget="_blank" href="/search/%E7%AC%94%E8%AE%B0/1.htm">笔记</a><a class="tag" taget="_blank" href="/search/chatgpt/1.htm">chatgpt</a><a class="tag" taget="_blank" href="/search/agi/1.htm">agi</a><a class="tag" taget="_blank" href="/search/embedding/1.htm">embedding</a><a class="tag" taget="_blank" href="/search/RAG/1.htm">RAG</a><a class="tag" taget="_blank" href="/search/prompt/1.htm">prompt</a> <div>大家好,我是同学小张,+v:jasper_8017一起交流,持续学习C++进阶、OpenGL、WebGL知识和AI大模型应用实战案例,持续分享,欢迎大家点赞+关注,共同学习和进步。RAG目前很火,但是也有一些不足的地方。有不足就有改进方法。本文我们来看一个方法:RAG-Fusion,理解其原理,并看一下其实现源码。文章目录0.RAG的不足1.RAG-Fusion原理概述2.步骤拆解与代码示例2.1</div> </li> <li><a href="/article/1902903949401321472.htm" title="2025年零基础入门学网络安全(详细),看这篇就够了" target="_blank">2025年零基础入门学网络安全(详细),看这篇就够了</a> <span class="text-muted">网安大师兄</span> <a class="tag" taget="_blank" href="/search/web%E5%AE%89%E5%85%A8/1.htm">web安全</a><a class="tag" taget="_blank" href="/search/%E5%AE%89%E5%85%A8/1.htm">安全</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C/1.htm">网络</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/1.htm">网络安全</a><a class="tag" taget="_blank" href="/search/%E5%AF%86%E7%A0%81%E5%AD%A6/1.htm">密码学</a> <div>基于入门网络安全/黑客打造的:黑客&网络安全入门&进阶学习资源包一、自学网络安全学习的误区和陷阱1.不要试图先成为一名程序员(以编程为基础的学习)再开始学习我在之前的回答中,我都一再强调不要以编程为基础再开始学习网络安全,一般来说,学习编程不但学习周期长,而且实际向安全过渡后可用到的关键知识并不多一般人如果想要把编程学好再开始学习网络安全往往需要花费很长时间,容易半途而废。而且学习编程只是工具不是</div> </li> <li><a href="/article/1902878105391919104.htm" title="深入理解正则表达式:语法全解析" target="_blank">深入理解正则表达式:语法全解析</a> <span class="text-muted">谢兴豪</span> <div>本文还有配套的精品资源,点击获取简介:正则表达式是一种用于文本匹配的模式,广泛应用在文本处理、数据验证等领域。本文将全面探讨正则表达式的语法细节,包括字符匹配、元字符、字符类、量词、分组与反向引用、选择与否定、位置锚点、预定义字符集、模式修饰符、回溯控制以及正向先行断言和正向后行断言。掌握这些知识有助于提高编程效率和代码质量。1.正则表达式简介正则表达式是IT行业中的“瑞士军刀”,它们在文本处理、</div> </li> <li><a href="/article/1902874322855325696.htm" title="服务器相关的硬件知识" target="_blank">服务器相关的硬件知识</a> <span class="text-muted">猿小喵</span> <a class="tag" taget="_blank" href="/search/%E8%BF%90%E7%BB%B4/1.htm">运维</a><a class="tag" taget="_blank" href="/search/%E6%9C%8D%E5%8A%A1%E5%99%A8/1.htm">服务器</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a> <div>网卡:网卡是计算机网络中用于实现计算机之间通信的硬件设备。它工作在OSI模型的第二层(链路层),通过电缆或无线信号与网络设备(如交换机、路由器)连接,带有芯片,可插拔。网卡的接口分为电口(如RJ45接口,用于连接网线)和光口(用于连接光模块和光纤)。根据传输协议,网卡可分为以太网卡、FC(FibreChannel)网卡和iSCSI网卡。以太网卡是最常见的类型,用于普通网络通信;FC网卡主要用于存储</div> </li> <li><a href="/article/1902874323593523200.htm" title="JavaScript 模块化语法 import、export详解" target="_blank">JavaScript 模块化语法 import、export详解</a> <span class="text-muted">qq39138814</span> <a class="tag" taget="_blank" href="/search/javascript/1.htm">javascript</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a><a class="tag" taget="_blank" href="/search/ecmascript/1.htm">ecmascript</a> <div>JavaScript模块化语法import、export详解1.为什么需要模块化?在JavaScript早期,所有代码都是写在一个全局作用域中,这样做的问题是:变量污染:所有变量、函数都是全局的,容易互相干扰。文件依赖管理困难:多个JS文件之间的依赖关系混乱,难以维护。代码复用困难:无法方便地拆分和复用代码。为了解决这些问题,模块化方案应运而生。2.JavaScript模块化的发展2.1早期的模块</div> </li> <li><a href="/article/1902871547186573312.htm" title="【动态规划】P6005 [USACO20JAN] Time is Mooney G|普及+" target="_blank">【动态规划】P6005 [USACO20JAN] Time is Mooney G|普及+</a> <span class="text-muted">软件架构师何志丹</span> <a class="tag" taget="_blank" href="/search/%23/1.htm">#</a><a class="tag" taget="_blank" href="/search/%E6%B4%9B%E8%B0%B7%E6%99%AE%E5%8F%8A%2B/1.htm">洛谷普及+</a><a class="tag" taget="_blank" href="/search/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/1.htm">动态规划</a><a class="tag" taget="_blank" href="/search/%E7%AE%97%E6%B3%95/1.htm">算法</a><a class="tag" taget="_blank" href="/search/c%2B%2B/1.htm">c++</a><a class="tag" taget="_blank" href="/search/%E6%B4%9B%E8%B0%B7/1.htm">洛谷</a><a class="tag" taget="_blank" href="/search/%E5%9B%BE%E8%AE%BA/1.htm">图论</a> <div>本文涉及知识点C++动态规划P6005[USACO20JAN]TimeisMooneyG题目描述Bessie正在安排前往牛尼亚的一次出差,那里有NNN(2≤N≤10002\leqN\leq10002≤N≤1000)个编号为1…N1\ldotsN1…N的城市,由MMM(1≤M≤20001\leqM\leq20001≤M≤2000)条单向的道路连接。Bessie每次访问城市iii都可以赚到mim_im</div> </li> <li><a href="/article/1902871038501384192.htm" title="精挑20题:MySQL 8.0高频面试题深度解析——掌握核心知识点、新特性和优化技巧" target="_blank">精挑20题:MySQL 8.0高频面试题深度解析——掌握核心知识点、新特性和优化技巧</a> <span class="text-muted">dblens 数据库管理和开发工具</span> <a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a><a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/%E9%9D%A2%E8%AF%95/1.htm">面试</a> <div>1.MySQL8.0中,为什么查询缓存被移除?答案:原因:查询缓存对频繁更新的表效果差,任何对该表的写操作都会清空所有相关缓存,导致缓存命中率低,反而增加开销。替代方案:使用应用层缓存(如Redis)。优化查询和索引,减少对缓存的依赖。MySQL8.0改进:通过索引优化、并行查询等提升性能,弥补查询缓存缺失的影响。2.InnoDB的行锁和表锁分别在什么场景下使用?答案:行锁:高并发场景下更新或查询</div> </li> <li><a href="/article/81.htm" title="java短路运算符和逻辑运算符的区别" target="_blank">java短路运算符和逻辑运算符的区别</a> <span class="text-muted">3213213333332132</span> <a class="tag" taget="_blank" href="/search/java%E5%9F%BA%E7%A1%80/1.htm">java基础</a> <div> /* * 逻辑运算符——不论是什么条件都要执行左右两边代码 * 短路运算符——我认为在底层就是利用物理电路的“并联”和“串联”实现的 * 原理很简单,并联电路代表短路或(||),串联电路代表短路与(&&)。 * * 并联电路两个开关只要有一个开关闭合,电路就会通。 * 类似于短路或(||),只要有其中一个为true(开关闭合)是</div> </li> <li><a href="/article/208.htm" title="Java异常那些不得不说的事" target="_blank">Java异常那些不得不说的事</a> <span class="text-muted">白糖_</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/exception/1.htm">exception</a> <div>一、在finally块中做数据回收操作 比如数据库连接都是很宝贵的,所以最好在finally中关闭连接。 JDBCAgent jdbc = new JDBCAgent(); try{ jdbc.excute("select * from ctp_log"); }catch(SQLException e){ ... }finally{ jdbc.close(); </div> </li> <li><a href="/article/335.htm" title="utf-8与utf-8(无BOM)的区别" target="_blank">utf-8与utf-8(无BOM)的区别</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/PHP/1.htm">PHP</a> <div>BOM——Byte Order Mark,就是字节序标记   在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输 字符"ZERO WIDTH NO-BREAK SPACE"。这样如</div> </li> <li><a href="/article/462.htm" title="JAVA Annotation之定义篇" target="_blank">JAVA Annotation之定义篇</a> <span class="text-muted">周凡杨</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E6%B3%A8%E8%A7%A3/1.htm">注解</a><a class="tag" taget="_blank" href="/search/annotation/1.htm">annotation</a><a class="tag" taget="_blank" href="/search/%E5%85%A5%E9%97%A8/1.htm">入门</a><a class="tag" taget="_blank" href="/search/%E6%B3%A8%E9%87%8A/1.htm">注释</a> <div>    Annotation: 译为注释或注解 An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, pa</div> </li> <li><a href="/article/589.htm" title="tomcat的多域名、虚拟主机配置" target="_blank">tomcat的多域名、虚拟主机配置</a> <span class="text-muted">g21121</span> <a class="tag" taget="_blank" href="/search/tomcat/1.htm">tomcat</a> <div>众所周知apache可以配置多域名和虚拟主机,而且配置起来比较简单,但是项目用到的是tomcat,配来配去总是不成功。查了些资料才总算可以,下面就跟大家分享下经验。 很多朋友搜索的内容基本是告诉我们这么配置: 在Engine标签下增面积Host标签,如下: <Host name="www.site1.com" appBase="webapps"</div> </li> <li><a href="/article/716.htm" title="Linux SSH 错误解析(Capistrano 的cap 访问错误 Permission )" target="_blank">Linux SSH 错误解析(Capistrano 的cap 访问错误 Permission )</a> <span class="text-muted">510888780</span> <a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/capistrano/1.htm">capistrano</a> <div> 1.ssh -v hdfs@192.168.18.133 出现 Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 错误 运行状况如下: OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013 debug1: Reading configuratio</div> </li> <li><a href="/article/843.htm" title="log4j的用法" target="_blank">log4j的用法</a> <span class="text-muted">Harry642</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/log4j/1.htm">log4j</a> <div>一、前言:     log4j 是一个开放源码项目,是广泛使用的以Java编写的日志记录包。由于log4j出色的表现,     当时在log4j完成时,log4j开发组织曾建议sun在jdk1.4中用log4j取代jdk1.4 的日志工具类,但当时jdk1.4已接近完成,所以sun拒绝使用log4j,当在java开发中</div> </li> <li><a href="/article/970.htm" title="mysql、sqlserver、oracle分页,java分页统一接口实现" target="_blank">mysql、sqlserver、oracle分页,java分页统一接口实现</a> <span class="text-muted">aijuans</span> <a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a><a class="tag" taget="_blank" href="/search/jave/1.htm">jave</a> <div> 定义:pageStart 起始页,pageEnd 终止页,pageSize页面容量 oracle分页:     select * from ( select mytable.*,rownum num from (实际传的SQL) where rownum<=pageEnd) where num>=pageStart sqlServer分页:  </div> </li> <li><a href="/article/1097.htm" title="Hessian 简单例子" target="_blank">Hessian 简单例子</a> <span class="text-muted">antlove</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/Web/1.htm">Web</a><a class="tag" taget="_blank" href="/search/service/1.htm">service</a><a class="tag" taget="_blank" href="/search/hessian/1.htm">hessian</a> <div>hello.hessian.MyCar.java package hessian.pojo; import java.io.Serializable; public class MyCar implements Serializable { private static final long serialVersionUID = 473690540190845543</div> </li> <li><a href="/article/1224.htm" title="数据库对象的同义词和序列" target="_blank">数据库对象的同义词和序列</a> <span class="text-muted">百合不是茶</span> <a class="tag" taget="_blank" href="/search/sql/1.htm">sql</a><a class="tag" taget="_blank" href="/search/%E5%BA%8F%E5%88%97/1.htm">序列</a><a class="tag" taget="_blank" href="/search/%E5%90%8C%E4%B9%89%E8%AF%8D/1.htm">同义词</a><a class="tag" taget="_blank" href="/search/ORACLE%E6%9D%83%E9%99%90/1.htm">ORACLE权限</a> <div>回顾简单的数据库权限等命令; 解锁用户和锁定用户 alter user scott account lock/unlock; //system下查看系统中的用户 select * dba_users; //创建用户名和密码 create user wj identified by wj; identified by //授予连接权和建表权 grant connect to </div> </li> <li><a href="/article/1351.htm" title="使用Powermock和mockito测试静态方法" target="_blank">使用Powermock和mockito测试静态方法</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90/1.htm">持续集成</a><a class="tag" taget="_blank" href="/search/%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/1.htm">单元测试</a><a class="tag" taget="_blank" href="/search/mockito/1.htm">mockito</a><a class="tag" taget="_blank" href="/search/Powermock/1.htm">Powermock</a> <div>        实例: package com.bijian.study; import static org.junit.Assert.assertEquals; import java.io.IOException; import org.junit.Before; import org.junit.Test; import or</div> </li> <li><a href="/article/1478.htm" title="精通Oracle10编程SQL(6)访问ORACLE" target="_blank">精通Oracle10编程SQL(6)访问ORACLE</a> <span class="text-muted">bijian1013</span> <a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/plsql/1.htm">plsql</a> <div>/* *访问ORACLE */ --检索单行数据 --使用标量变量接收数据 DECLARE v_ename emp.ename%TYPE; v_sal emp.sal%TYPE; BEGIN select ename,sal into v_ename,v_sal from emp where empno=&no; dbms_output.pu</div> </li> <li><a href="/article/1605.htm" title="【Nginx四】Nginx作为HTTP负载均衡服务器" target="_blank">【Nginx四】Nginx作为HTTP负载均衡服务器</a> <span class="text-muted">bit1129</span> <a class="tag" taget="_blank" href="/search/nginx/1.htm">nginx</a> <div> Nginx的另一个常用的功能是作为负载均衡服务器。一个典型的web应用系统,通过负载均衡服务器,可以使得应用有多台后端服务器来响应客户端的请求。一个应用配置多台后端服务器,可以带来很多好处:   负载均衡的好处 增加可用资源 增加吞吐量 加快响应速度,降低延时 出错的重试验机制 Nginx主要支持三种均衡算法: round-robin l</div> </li> <li><a href="/article/1732.htm" title="jquery-validation备忘" target="_blank">jquery-validation备忘</a> <span class="text-muted">白糖_</span> <a class="tag" taget="_blank" href="/search/jquery/1.htm">jquery</a><a class="tag" taget="_blank" href="/search/css/1.htm">css</a><a class="tag" taget="_blank" href="/search/F%23/1.htm">F#</a><a class="tag" taget="_blank" href="/search/Firebug/1.htm">Firebug</a> <div>留点学习jquery validation总结的代码:   function checkForm(){ validator = $("#commentForm").validate({// #formId为需要进行验证的表单ID errorElement :"span",// 使用"div"标签标记错误, 默认:&</div> </li> <li><a href="/article/1859.htm" title="solr限制admin界面访问(端口限制和http授权限制)" target="_blank">solr限制admin界面访问(端口限制和http授权限制)</a> <span class="text-muted">ronin47</span> <a class="tag" taget="_blank" href="/search/%E9%99%90%E5%AE%9AIp%E8%AE%BF%E9%97%AE/1.htm">限定Ip访问</a> <div>solr的管理界面可以帮助我们做很多事情,但是把solr程序放到公网之后就要限制对admin的访问了。 可以通过tomcat的http基本授权来做限制,也可以通过iptables防火墙来限制。 我们先看如何通过tomcat配置http授权限制。 第一步: 在tomcat的conf/tomcat-users.xml文件中添加管理用户,比如: <userusername="ad</div> </li> <li><a href="/article/1986.htm" title="多线程-用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1" target="_blank">多线程-用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1</a> <span class="text-muted">bylijinnan</span> <a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E5%A4%9A%E7%BA%BF%E7%A8%8B/1.htm">多线程</a> <div> public class IncDecThread { private int j=10; /* * 题目:用JAVA写一个多线程程序,写四个线程,其中二个对一个变量加1,另外二个对一个变量减1 * 两个问题: * 1、线程同步--synchronized * 2、线程之间如何共享同一个j变量--内部类 */ public static </div> </li> <li><a href="/article/2113.htm" title="买房历程" target="_blank">买房历程</a> <span class="text-muted">cfyme</span> <div>    2015-06-21: 万科未来城,看房子   2015-06-26: 办理贷款手续,贷款73万,贷款利率5.65=5.3675   2015-06-27: 房子首付,签完合同   2015-06-28,央行宣布降息 0.25,就2天的时间差啊,没赶上。   首付,老婆找他的小姐妹接了5万,另外几个朋友借了1-</div> </li> <li><a href="/article/2240.htm" title="[军事与科技]制造大型太空战舰的前奏" target="_blank">[军事与科技]制造大型太空战舰的前奏</a> <span class="text-muted">comsci</span> <a class="tag" taget="_blank" href="/search/%E5%88%B6%E9%80%A0/1.htm">制造</a> <div>        天气热了........空调和电扇要准备好..........        最近,世界形势日趋复杂化,战争的阴影开始覆盖全世界..........        所以,我们不得不关</div> </li> <li><a href="/article/2367.htm" title="dateformat" target="_blank">dateformat</a> <span class="text-muted">dai_lm</span> <a class="tag" taget="_blank" href="/search/DateFormat/1.htm">DateFormat</a> <div> "Symbol Meaning Presentation Ex." "------ ------- ------------ ----" "G era designator (Text) AD" "y year</div> </li> <li><a href="/article/2494.htm" title="Hadoop如何实现关联计算" target="_blank">Hadoop如何实现关联计算</a> <span class="text-muted">datamachine</span> <a class="tag" taget="_blank" href="/search/mapreduce/1.htm">mapreduce</a><a class="tag" taget="_blank" href="/search/hadoop/1.htm">hadoop</a><a class="tag" taget="_blank" href="/search/%E5%85%B3%E8%81%94%E8%AE%A1%E7%AE%97/1.htm">关联计算</a> <div>    选择Hadoop,低成本和高扩展性是主要原因,但但它的开发效率实在无法让人满意。     以关联计算为例。     假设:HDFS上有2个文件,分别是客户信息和订单信息,customerID是它们之间的关联字段。如何进行关联计算,以便将客户名称添加到订单列表中?   &nbs</div> </li> <li><a href="/article/2621.htm" title="用户模型中修改用户信息时,密码是如何处理的" target="_blank">用户模型中修改用户信息时,密码是如何处理的</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/yii/1.htm">yii</a> <div>当我添加或修改用户记录的时候对于处理确认密码我遇到了一些麻烦,所有我想分享一下我是怎么处理的。 场景是使用的基本的那些(系统自带),你需要有一个数据表(user)并且表中有一个密码字段(password),它使用 sha1、md5或其他加密方式加密用户密码。 面是它的工作流程: 当创建用户的时候密码需要加密并且保存,但当修改用户记录时如果使用同样的场景我们最终就会把用户加密过的密码再次加密,这</div> </li> <li><a href="/article/2748.htm" title="中文 iOS/Mac 开发博客列表" target="_blank">中文 iOS/Mac 开发博客列表</a> <span class="text-muted">dcj3sjt126com</span> <a class="tag" taget="_blank" href="/search/Blog/1.htm">Blog</a> <div>  本博客列表会不断更新维护,如果有推荐的博客,请到此处提交博客信息。 本博客列表涉及的文章内容支持 定制化Google搜索,特别感谢 JeOam 提供并帮助更新。 本博客列表也提供同步更新的OPML文件(下载OPML文件),可供导入到例如feedly等第三方定阅工具中,特别感谢 lcepy 提供自动转换脚本。这里有导入教程。 </div> </li> <li><a href="/article/2875.htm" title="js去除空格,去除左右两端的空格" target="_blank">js去除空格,去除左右两端的空格</a> <span class="text-muted">蕃薯耀</span> <a class="tag" taget="_blank" href="/search/%E5%8E%BB%E9%99%A4%E5%B7%A6%E5%8F%B3%E4%B8%A4%E7%AB%AF%E7%9A%84%E7%A9%BA%E6%A0%BC/1.htm">去除左右两端的空格</a><a class="tag" taget="_blank" href="/search/js%E5%8E%BB%E6%8E%89%E6%89%80%E6%9C%89%E7%A9%BA%E6%A0%BC/1.htm">js去掉所有空格</a><a class="tag" taget="_blank" href="/search/js%E5%8E%BB%E9%99%A4%E7%A9%BA%E6%A0%BC/1.htm">js去除空格</a> <div>js去除空格,去除左右两端的空格 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>&g</div> </li> <li><a href="/article/3002.htm" title="SpringMVC4零配置--web.xml" target="_blank">SpringMVC4零配置--web.xml</a> <span class="text-muted">hanqunfeng</span> <a class="tag" taget="_blank" href="/search/springmvc4/1.htm">springmvc4</a> <div>servlet3.0+规范后,允许servlet,filter,listener不必声明在web.xml中,而是以硬编码的方式存在,实现容器的零配置。 ServletContainerInitializer:启动容器时负责加载相关配置 package javax.servlet; import java.util.Set; public interface ServletContainer</div> </li> <li><a href="/article/3129.htm" title="《开源框架那些事儿21》:巧借力与借巧力" target="_blank">《开源框架那些事儿21》:巧借力与借巧力</a> <span class="text-muted">j2eetop</span> <a class="tag" taget="_blank" href="/search/%E6%A1%86%E6%9E%B6/1.htm">框架</a><a class="tag" taget="_blank" href="/search/UI/1.htm">UI</a> <div>同样做前端UI,为什么有人花了一点力气,就可以做好?而有的人费尽全力,仍然错误百出?我们可以先看看几个故事。 故事1:巧借力,乌鸦也可以吃核桃 有一个盛产核桃的村子,每年秋末冬初,成群的乌鸦总会来到这里,到果园里捡拾那些被果农们遗落的核桃。 核桃仁虽然美味,但是外壳那么坚硬,乌鸦怎么才能吃到呢?原来乌鸦先把核桃叼起,然后飞到高高的树枝上,再将核桃摔下去,核桃落到坚硬的地面上,被撞破了,于是,</div> </li> <li><a href="/article/3256.htm" title="JQuery EasyUI 验证扩展" target="_blank">JQuery EasyUI 验证扩展</a> <span class="text-muted">可怜的猫</span> <a class="tag" taget="_blank" href="/search/jquery/1.htm">jquery</a><a class="tag" taget="_blank" href="/search/easyui/1.htm">easyui</a><a class="tag" taget="_blank" href="/search/%E9%AA%8C%E8%AF%81/1.htm">验证</a> <div>  最近项目中用到了前端框架-- EasyUI,在做校验的时候会涉及到很多需要自定义的内容,现把常用的验证方式总结出来,留待后用。   以下内容只需要在公用js中添加即可。   使用类似于如下: <input class="easyui-textbox" name="mobile" id="mobile&</div> </li> <li><a href="/article/3383.htm" title="架构师之httpurlconnection----------读取和发送(流读取效率通用类)" target="_blank">架构师之httpurlconnection----------读取和发送(流读取效率通用类)</a> <span class="text-muted">nannan408</span> <div>1.前言.    如题. 2.代码. /* * Copyright (c) 2015, S.F. Express Inc. All rights reserved. */ package com.test.test.test.send; import java.io.IOException; import java.io.InputStream</div> </li> <li><a href="/article/3510.htm" title="Jquery性能优化" target="_blank">Jquery性能优化</a> <span class="text-muted">r361251</span> <a class="tag" taget="_blank" href="/search/JavaScript/1.htm">JavaScript</a><a class="tag" taget="_blank" href="/search/jquery/1.htm">jquery</a> <div>一、注意定义jQuery变量的时候添加var关键字 这个不仅仅是jQuery,所有javascript开发过程中,都需要注意,请一定不要定义成如下: $loading = $('#loading'); //这个是全局定义,不知道哪里位置倒霉引用了相同的变量名,就会郁闷至死的 二、请使用一个var来定义变量 如果你使用多个变量的话,请如下方式定义: . 代码如下: var page </div> </li> <li><a href="/article/3637.htm" title="在eclipse项目中使用maven管理依赖" target="_blank">在eclipse项目中使用maven管理依赖</a> <span class="text-muted">tjj006</span> <a class="tag" taget="_blank" href="/search/eclipse/1.htm">eclipse</a><a class="tag" taget="_blank" href="/search/maven/1.htm">maven</a> <div>概览: 如何导入maven项目至eclipse中 建立自有Maven  Java类库服务器 建立符合maven代码库标准的自定义类库 Maven在管理Java类库方面有巨大的优势,像白衣所说就是非常“环保”。 我们平时用IDE开发都是把所需要的类库一股脑的全丢到项目目录下,然后全部添加到ide的构建路径中,如果用了SVN/CVS,这样会很容易就 把</div> </li> <li><a href="/article/3764.htm" title="中国天气网省市级联页面" target="_blank">中国天气网省市级联页面</a> <span class="text-muted">x125858805</span> <a class="tag" taget="_blank" href="/search/%E7%BA%A7%E8%81%94/1.htm">级联</a> <div>1、页面及级联js <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> &l</div> </li> </ul> </div> </div> </div> <div> <div class="container"> <div class="indexes"> <strong>按字母分类:</strong> <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank">其他</a> </div> </div> </div> <footer id="footer" class="mb30 mt30"> <div class="container"> <div class="footBglm"> <a target="_blank" href="/">首页</a> - <a target="_blank" href="/custom/about.htm">关于我们</a> - <a target="_blank" href="/search/Java/1.htm">站内搜索</a> - <a target="_blank" href="/sitemap.txt">Sitemap</a> - <a target="_blank" href="/custom/delete.htm">侵权投诉</a> </div> <div class="copyright">版权所有 IT知识库 CopyRight © 2000-2050 E-COM-NET.COM , All Rights Reserved. <!-- <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备09083238号</a><br>--> </div> </div> </footer> <!-- 代码高亮 --> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script> <script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script> <link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/> <script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script> </body> </html>