《javascript》ppt教程-微盤下載
第一章:javascript語法結構
【web前端全套】鏈接:https://pan.baidu.com/s/16K9bdHmXf4EEWBtsSH9r5A 密碼:mb4q
1.ps 鏈接:
https://pan.baidu.com/s/1mjYbcJA
密碼:pqdt
2.HTML5+CSS3從入門到精通.iso 鏈接:
https://pan.baidu.com/s/1cUHz3o
密碼:gdyw
3.Javascript視頻教程 鏈接: https://pan.baidu.com/s/1i6eC5YP
密碼:ku5k
4.Jquery視頻教程 鏈接:https://pan.baidu.com/s/17DbNSnR3q5OGSjdw9tvCEA
密碼:twcp
5.Bootstrap視頻教程 鏈接: https://pan.baidu.com/s/1jKb2UDS
密碼:wr8c
6.Vue.js 鏈接:
https://pan.baidu.com/s/1slYqBa1
密碼:75xd
7.Smarty模板引擎視頻教程 鏈接: https://pan.baidu.com/s/1smewom5
密碼:v8vp
8.Nodejs 鏈接: https://pan.baidu.com/s/1sm509GD
密碼:fux1
9.Mysql視頻教程 鏈接: https://pan.baidu.com/s/1kVTdjcr
密碼:e37d
10.PHP視頻教程 鏈接: https://pan.baidu.com/s/1kWE0qZP
密碼:xc4q
11.oracle視頻教程 鏈接: https://pan.baidu.com/s/1i6jwpal
密碼:hk6p
12.nginx視頻教程 鏈接: https://pan.baidu.com/s/1nxccLg1
密碼:x2q8
13.ajax 鏈接:
https://pan.baidu.com/s/1dHctdXv
密碼:jfwf
14.mongodb視頻教程 鏈接: https://pan.baidu.com/s/1c3EE7U4
密碼:7v8b
15.Angular.js鏈接: https://pan.baidu.com/s/1jJhYrEu
密碼:ddci
16.React鏈接:
https://pan.baidu.com/s/1nwnrQxb
密碼:942g
17.dedeCMS織夢視頻 鏈接: https://pan.baidu.com/s/1bpQBCbP
密碼:ikns
18.ThinkPHP視頻教程 鏈接: https://pan.baidu.com/s/1ggiKvYV
密碼:7p5h
19.案例和總結 鏈接: https://pan.baidu.com/s/1eTSNB5C
密碼:5rx7
20.天貓商城購物車實戰 鏈接: https://pan.baidu.com/s/1smVjb7v
密碼:cu4c
21.Java學科鏈接:https://pan.baidu.com/s/1DtnDNv1p9ENZETLqIuKytw
密碼:82lk
22.Python鏈接:https://pan.baidu.com/s/1scBXSLdiTs9SBixdkykflw
密碼:ck51
23.nodejs微信開發鏈接:https://pan.baidu.com/s/15tYBZ2yVz9aPO57LKqt7Mg
密碼:jrwb
java300集鏈接:https://pan.baidu.com/s/1nh-hcdQfdWlEjmSAYO4uWQ 密碼:2iza
Python鏈接:https://pan.baidu.com/s/1m4cMqkOYRUFv-2T5Rm6Qmg 密碼:2kpz
Web前端:鏈接:https://pan.baidu.com/s/1-2tZKmjSRIbBaxwuZjnsnA 密碼:i5f1
大數據鏈接:https://pan.baidu.com/s/1eUz4AB0InYRoJddAG-6j6Q 密碼:quo9
1.1字符集
Javascript是用unicode字符集編寫的,unicode是ascii和latin-1的超集,并支持地球上幾乎所有在用的語言。
1.2 區分大小寫
Js是區分大小寫的。
1.3注釋
// 單行注釋
/*
*多行注釋
*/
1.4 標識符和保留字
標識符用來對變量和函數進行命名,js標識符必須是字母、下劃線或美元符開頭,后續的字符可以是字母、數字、下劃線和美元符。保留字不能當做標識符或函數名。
JavaScript 保留關鍵字
JavaScript 對象、屬性和方法
您也應該避免使用 JavaScript 內置的對象、屬性和方法的名稱作為 Javascript 的變量或函數名:
HTML 事件句柄
除此之外,您還應該避免使用 HTML 事件句柄的名稱作為 Javascript 的變量及函數名。
實例:
1.5 命名習慣
類型 | 前綴 | 例子 |
數組 | a | aArray |
布爾值 | b | bMale |
浮點 | f | fTax |
函數 | fn | fnSwap |
整型 | i | iAge |
對象 | o | oCar |
正則 | re | rePattern |
字符串 | s | sUniversity |
第二章:類型、值和變量
2.1 數字
2.1.1 整型
2.1.2 浮點類型
3.14
.3333
6.02e23 //6.02*1023
1.4732e-32 //1.4732*10-32
2.2文本
2.2.1 單引號,雙引號
在js程序中的字符串是由單引號或者雙引號括起來的字符序列,由單引號定界的字符串中可以包含雙引號,由雙引號定界的字符串中也可以包含單引號。
var s='tesing';
var s='name="myfrom"';
var s="you’re right,it cant’t be quote ";//如果想要里面的'作為普通字符串輸出則需要進行轉義處理,轉義方式 \’
2.2.2 字符串的使用
var s="hello world";
console.log(s.length);//獲取長度
console.log(s.charAt(0));// "h" 第一個字符
console.log(s.substring(1));
console.log(s.substring(1,4));//"ell" 第2~4個字符
console.log(s.slice(1,4));//同上
console.log(s.slice(-3));//"rld" 最后三個字符
console.log(s.indexOf("l"));//第一次出現的索引
console.log(s.lastIndexOf("l"));//最后一次出現的索引
console.log(s.indexOf("l",3));//在位置3及之后首次出現字符l的位置
console.log(s.split(","));//變數組
console.log(s.toUpperCase());//變大寫
console.log(s.toLowerCase());//變小寫
console.log(s.replace("h","H"));//替換
console.log(s[0]);//es5中,字符串可以當做只讀數組
console.log(s[s.length-1]);
2.3 布爾類型
2.4 null和undefined
Undefined類型只有一個值,即undefined。當聲明的變量還未被初始化時,變量的默認值為undefined。
Null類型也只有一個值,即null。null用來表示尚未存在的對象,常用來表示函數企圖返回一個不存在的對象。
var oValue;
alert(oValue == undefined); //output "true"
這段代碼顯示為true,代表oVlaue的值即為undefined,因為我們沒有初始化它。
alert(null == document.getElementById('notExistElement'));
當頁面上不存在id為"notExistElement"的DOM節點時,這段代碼顯示為"true",因為我們嘗試獲取一個不存在的對象。
alert(typeof undefined); //output "undefined"
alert(typeof null); //output "object"
第一行代碼很容易理解,undefined的類型為Undefined;第二行代碼卻讓人疑惑,為什么null的類型又是Object了呢?其實這是JavaScript最初實現的一個錯誤,后來被ECMAScript沿用下來。在今天我們可以解釋為,null即是一個不存在的對象的占位符,但是在實際編碼時還是要注意這一特性。
alert(null == undefined); //output "true"
ECMAScript認為undefined是從null派生出來的,所以把它們定義為相等的。但是,如果在一些情況下,我們一定要區分這兩個值,那應該怎么辦呢?可以使用下面的兩種方法。
alert(null === undefined); //output "false"
alert(typeof null == typeof undefined); //output "false"
使用typeof方法在前面已經講過,null與undefined的類型是不一樣的,所以輸出"false"。而===代表絕對等于,在這里null === undefined輸出false。
2.5 類型轉化
值 | 轉化為字符串 | 數字 | 布爾值 |
undefined | NaN | false | |
null | null | 0 | false |
true | “true” | 1 | |
false | “false” | 0 | |
“” | 0 | false | |
“1.2”(非空,數字) | 1.2 | true | |
“one”(非空,非數字) | NaN | true | |
0 | “0” | 0 | false |
-0 | “0” | false | |
NaN | “NaN” | false | |
Infinity(存放無窮大的數值) Eg: var t1=1.7976931348623157E+10308 document.write(t1) //結果Infinity | “infinity” | true | |
- Infinity | “-Infinity” | true | |
1 | “1” | true | |
{} (任意對象) | true | ||
[](任意數組) | “” | 0 | true |
function(){} | NaN | true |
2.6 變量作用域
當代碼進入到了<script>或者一個function里面就是進入到了一個作用域,進入到作用域后,瀏覽器里面的js解析器,就會執行兩個步驟:
1) 去找var 、function、方法參數,進行js預解析。
a=undefined;//所有的變量,在正式運行代碼之前,都會提前賦一個值:未定義
2) 逐行解讀代碼。
當看到 表達式:+ - * / % = ++ -- ! 參數…… 就會去修改步驟1里面的內容。
第三章:表達式和運算符
名稱 | 含義 | 例子 |
= == === | ||
< > <= >= | ||
in | 左邊為字符串或可轉成字符串,右邊為一個對象,左邊的屬性存在于右邊對象,則返還ok | function Test( ) { var o={x:1,y:2}; if("x" in o){}//true if("z" in o){}//false if("toString" in o){}//true,對象繼承了toString方法 } |
instanceof | 左側對象為右側類的實例,則返回true
| var d=new Data(); if(d instanceof Date){}//true if(z instanceof Date){}//false
|
&&、 || 、 ! | ||
+=、-=、*=、/=、%=、^= | ||
?: | ||
typeof | ||
delete | function Test( ) { var o={x:1,y:2}; delete o.x; if("x" in o){}//false }
|
第四章:語句
名稱 | 含義 | 例子 |
if/else if/else | ||
switch case | ||
break/continue/return | ||
for | ||
while | ||
do while | ||
for in | function Test( ) { var o={x:1,y:2}; for(i in o){ console.log(o[i]); } } | |
try..catch..finally | function Test( ) { try{ var n=Number(prompt("請輸入一個正整數")); var f=factorial(n); alert(n+"!="+f); } catch(ex){ alert(ex); } finally{ aler("不管是否有異常,都顯示"); } } function factorial(x) { if(x<0){ throw new Error("x不能為負數"); } if(isNaN(x)){ throw new Error("你輸入的不是一個數字"); } for(var f=1;x>1;x--){ f*=x; } return f; }
| |
with | function Test() { // document.forms[0].username.value = "aa"; // document.forms[0].pwd.value = "aa"; // document.forms[0].qq.value = "aa"; // document.forms[0].realname.value = "aa"; // document.forms[0].tel.value = "aa";
//簡寫方式 with(document.forms[0]){ username.value="aa"; pwd.value="aa"; qq.value="aa"; tel.value="aa"; realname.value="aa"; } } |
第五章:面向對象
5.1 創建對象
5.1.1 對象直接量
var empty={};//沒有任何屬性的對象
var point={x:0,y:0};//兩個屬性
var point2={x:point.x,y:point.y};//更復雜的值
var book={
"main title":"javascript",//屬性妹子里有空格,必須用字符串表示
"sub-title":"xxxxxx",//屬性名字里有連接符的,必須用字符串表示
"for":"alllx",//屬性名如果是保留字,必須用引號
author:{
firstname:"ddd",
surnmae:"fff"
}
}
5.1.2 new一個對象/原型
function createPerson(name, sex) {//構造方法
var obj = new Object();
obj.name = name;
obj.sex = sex;
obj.showName = function () {
alert("我的名字叫" + this.name);
}
obj.showSex = function () {
alert("我的性別是" + this.sex);
}
return obj;
}
var p1=createPerson("blue","男");
var p2=createPerson("leo","女");
p1.showName();
p1.showSex();
p2.showName();
p2.showSex();
以上代碼的問題:每創建一個對象的時候,就會創建一次自己的showName和showSex方法,很浪費資源。類似下面:
var arr1 = new Array(12, 5, 7, 2);
var arr2 = new Array(22, 3, 4, 2);
// arr1.sum=function(){
// var result=0;
// var i=0;
// for(i=0;i<this.length;i++){
// result+=this[i];
// }
// return result;
// }
// alert(arr1.sum());
// alert(arr2.sum());//此時就會報錯
Array.prototype.sum = function () {//原型方法
var result = 0;
var i = 0;
for (i = 0; i < this.length; i++) {
result += this[i];
}
return result;
}
alert(arr1.sum());
alert(arr2.sum());//此時都會有
所以創建對象通常的寫法是:
function Person(name, sex) {
//調用構造方法new了之后,系統內部 var this=new Object();
this.name = name;
this.sex = sex;
//調用構造方法new了之后,系統內部 return this;
}
Person.prototype.showName = function () {
alert("我的名字叫:" + this.name);
}
Person.prototype.showSex = function () {
alert("我的性別是:" + this.sex);
}
var p1 = new Person("blue", "男");
var p2 = new Person("leo", "女");
p1.showName();
p1.showSex();
p2.showName();
p2.showSex();
優先級:
Array.prototype.a=12;//相當于給div加class
var arr=[1,2,3];
alert(arr.a);//12
arr.a=5;//相當于給div內部加 style=""
alert(arr.a);//5
delete arr.a;
alert(arr.a);//12
5.2 刪除屬性
var o={x:1};
delete o.x;//刪除x,返回true
delete o.toString();//什麼也沒做(繼承來的不能刪除,要刪除繼承來的方法或者屬性,
//必須從定義這個屬性的原型對象上刪除它,而且會影響到所有繼承這個原型的對象)
5.3 檢測屬性
var o={x:1};
//方式一:
"x" in o;//true
"y" in o;//false
"toString" in o;//true , o繼承toString屬性
//方式二:
o.hasOwnProperty("x");//true
o.hasOwnProperty("y");//false
o.hasOwnProperty("toString");//false
//方式三:
o.x!==undefined;//true
o.y!==undefined;//false
o.toString!==undefined;//true
5.4 關于this的問題
function Aaa(){
this.a=12;
//setInterval(this.show,1000);//setInterval和setTimeout的回調函數中this的指向都是window。
//這是因為JS的定時器方法是定義在window下的,即window.setInterval,所以此時的this.show調用的不是Aaa里面的show
var _this=this;
setInterval(function(){//這樣寫才是調用的是Aaa里面的show
_this.show();
},1000);
}
Aaa.prototype.show=function(){
alert(this.a);
}
var obj=new Aaa();
obj.show();
function Bbb() {
this.b = 5;
//document.getElementById("btn1").onclick=this.show;//此處的this表示的是按鈕,不是Bbb對象
var _this = this;
document.getElementById("btn1").onclick = function () {
_this.show();//這時調用的才是Bbb的對象
}
}
Bbb.prototype.show = function () {
alert(this.b);
}
window.onload = function () {
new Bbb();
}
5.5 call方法使用
調用一個對象的一個方法,以另一個對象替換當前對象。
function Cat(){
}
Cat.prototype={
food:"fish",
say:function(){
alert("i love "+this.food);
}
}
var blackCat=new Cat();
blackCat.say();
//當需要一條黑狗也說他喜歡什么的時候,
blackDog={food:"bone"};
//我們不想對他重新定義say方法,那么我們可以通過call用blackCat的say方法
blackCat.say.call(blackDog);
function A(name) {//A 類
this.name = name;
this.showName = function () {
alert(this.name);
}
}
function B(name, age) { //B 類
this.name = name;
this.age = age;
}
var a = new A("aaa"); //a對象是有showName方法的
var b = new B('bbb', 10);//b對象沒有showName方法
a.showName.call(b);//但是這里showName,顯示的是b的名字,也就是b調用了a的showName方法
call實現繼承:
function A(name) {//A 類 還是這個例子,A有showName
this.name = name;
this.showName = function () {
alert(this.name);
}
}
function B(name, age) { //B 類,
//B類中通過A.call,相當于執行了A(name),但在A中所有this其實都是B。那么B就有name這個屬性,同時也擁有showName方法了。
A.call(this, name);
//A.apply(this,[name]);//和上面的效果是一樣的,區別是只是傳入的參數列表形式不同:如 func.call(func1,var1,var2,var3) 對應的apply寫法為:func.apply(func1,[var1,var2,var3]),也就是說:call調用的為單個,apply調用的參數為數組
this.age = age;
}
var b = new B('bbb', 10);
b.showName();//B繼承了A,所以b也有showName方法。
5.6 typeof 和instanceof區別
//typeof會返回一個變量的基本類型,只有以下幾種:number,boolean,string,object,undefined,function;例:
alert(typeof (1));//number
alert(typeof ("abc"));//string
alert(typeof (true));//boolean
alert(typeof (m));//undefined
//instanceof返回的是一個布爾值,如:
var a = {};
alert(a instanceof Object); //true
var b = [];
alert(b instanceof Array); //true
function Person(){}
var p=new Person();
alert(p instanceof Person);
//需要注意的是,instanceof只能用來判斷對象和函數,不能用來判斷字符串和數字等
5.7 系統對象
本地對象(常用對象):Object、Function、Array、String、Boolean、Number、Date、RegExp、Error
內置對象(靜態對象):Golobal、Math
宿主對象(由瀏覽器提供的對象)
DOM -----document
BOM-----window
5.8 json
JSON字符串:
var str1 = '{ "name": "cxh", "sex": "man" }';
JSON對象:
var str2 = { "name": "cxh", "sex": "man" };
1)jQuery插件支持的轉換方式:
$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以將json字符串轉換成json對象
2)瀏覽器支持的轉換方式(Firefox,chrome,opera,safari,ie9,ie8)等瀏覽器:
JSON.parse(jsonstr); //可以將json字符串轉換成json對象
JSON.stringify(jsonobj); //可以將json對象轉換成json對符串
注:ie8(兼容模式),ie7和ie6沒有JSON對象,推薦采用JSON官方的方式,引入json.js。
3) Javascript支持的轉換方式:
eval('(' + jsonstr + ')'); //可以將json字符串轉換成json對象,注意需要在json字符外包裹一對小括號
注:ie8(兼容模式),ie7和ie6也可以使用eval()將字符串轉為JSON對象,但不推薦這些方式,這種方式不安全eval會執行json串中的表達式。
JSON官方的轉換方式:
http://www.json.org/提供了一個json.js,這樣ie8(兼容模式),ie7和ie6就可以支持JSON對象以及其stringify()和parse()方法;
可以在https://github.com/douglascrockford/JSON-js上獲取到這個js,一般現在用json2.js。
第六章:數組
6.1 創建數組
var empty=[];
var primes=[2,3,4,5,6];
var a=new Array();//等同[]
var a=new Array(10);//規定了長度
var a=new Array(2,3,4,5,6);//等同[2,3,4,5,6]
var a=[[1,2],[3,4,6,7,8],[34,3,2]];//多維數組
6.2 數組長度
length屬性
6.3 數組元素刪除和添加
var a=[];
a.push("zero");//添加一個,push() 方法可向數組的末尾添加一個或多個元素,并返回新的長度。
a.push("one","two");//添加兩個
delete a[1];
"one" in a;//false
alert(a.pop());//pop() 方法用于刪除并返回數組的最后一個元素。
6.4 遍歷數組
var a=[2,3,4,5,6,7,8,2];
var iSum=0;
//方式一
for(var i=0;i<a.length;i++){
iSum+=a[i];
}
//方式二
a.forEach(function(i){
iSum+=i;
});
6.5 數組方法
6.6 數組類型
Array.isArray([]);//true
6.7 作為數組的字符串
var s=”test”;
s.charAt(0);//“t”
s[1];//”e”
第七章:正則表達式
7.1 正則表達式定義
js 正則表達式用RegExp對象表示,可以使用RegExp()構造函數創建RegExp對象,不過RegExp對象更多是通過一種特殊的直接量語法來創建,正則表達式直接量定義為包含在一對斜杠/之間的字符,如:
var pattern=/s$/
等價于:
var pattern=new RegExp(“s$”);
7.2 字符類
正則 | 含義 | 說明 |
[…] | 方括號里的任意字符 | |
[^…] | 不在方括號內的任意字符 | |
. | 換行符以外的任意字符 | |
\w | 匹配所有的字母和數字 | 等價于[a-zA-Z0-9 ] |
\W | 匹配所有除字母和數字以外的字符 | 等價于[^a-zA-Z0-9 ] |
\s | 空白符 | |
\S | 空白符以外的 | |
\d | 數字 | 等價于[0-9] |
\D | 數字以外的 |
7.3 重復
正則 | 含義 | 說明 |
{n,m} | 匹配至少出現n次,不超過m次 | |
{n,} | 至少出現n次 | |
{n} | 出現n次 | |
? | 出現0次或者1次 | |
+ | 出現1次或多次 | |
* | 0次或多次 |
如:
/\d{2,4}/ 匹配2~4個數字
/\w{3}\d?/ 匹配三個單詞和一個可選的數字
/\s+java\s+/ 匹配前后哦帶有一個或多個空格的字符串java
/[^(]*/ 匹配一個或多個非左括號的字符
非貪婪重復
“{m,n}?”、“{m,}?”、“??”、“*?”和“+?”。
源字符串:aa<div>test1</div>bb<div>test2</div>cc
正則表達式一:<div>.*</div>
匹配結果一:<div>test1</div>bb<div>test2</div>
正則表達式二:<div>.*?</div>
匹配結果二:<div>test1</div>(這里指的是一次匹配結果,所以沒包括<div>test2</div>)
根據上面的例子,從匹配行為上分析一下,什是貪婪與非貪婪模式。
正則表達式一采用的是貪婪模式,在匹配到第一個“</div>”時已經可以使整個表達式匹配成功,但是由于采用的是貪婪模式,所以仍然要向右嘗試匹配,查看是否還有更長的可以成功匹配的子串,匹配到第二個“</div>”后,向右再沒有可以成功匹配的子串,匹配結束,匹配結果為“<div>test1</div>bb<div>test2</div>”。當然,實際的匹配過程并不是這樣的,后面的匹配原理會詳細介紹。
僅從應用角度分析,可以這樣認為,貪婪模式,就是在整個表達式匹配成功的前提下,盡可能多的匹配,也就是所謂的“貪婪”,通俗點講,就是看到想要的,有多少就撿多少,除非再也沒有想要的了。
正則表達式二采用的是非貪婪模式,在匹配到第一個“</div>”時使整個表達式匹配成功,由于采用的是非貪婪模式,所以結束匹配,不再向右嘗試,匹配結果為“<div>test1</div>”。
僅從應用角度分析,可以這樣認為,非貪婪模式,就是在整個表達式匹配成功的前提下,盡可能少的匹配,也就是所謂的“非貪婪”,通俗點講,就是找到一個想要的撿起來就行了,至于還有沒有沒撿的就不管了。
var pattern = /8(.*)8/;// 使用了貪婪,匹配到了google8 8google8 8google
var str = '8google8 8google8 8google8';
document.write(str.replace(pattern, '<strong>$1</strong>')) //$1 google8 8google8 8google
var pattern = /8(.*?)8/;// 使用了貪婪,匹配到了google8 8google8 8google
var str = '8google8 8google8 8google8';
document.write(str.replace(pattern, '<strong>$1</strong>')) //$1只匹配到了第一個google
var pattern = /8(.*?)8/g;// 使用了貪婪,匹配到了google8 8google8 8google
var str = '8google8 8google8 8google8';
document.write(str.replace(pattern, '<strong>$1</strong>'))//匹配到了所有的google
7.4 js正則標志/g /i /m的用法
js正則標志/g,/i,/m說明
/g 表示該表達式將用來在輸入字符串中查找所有可能的匹配,返回的結果可以是多個。如果不加/g最多只會匹配一個
/i 表示匹配的時候不區分大小寫
/m 表示多行匹配,什么是多行匹配呢?就是匹配換行符兩端的潛在匹配。影響正則中的^$符號
1. <script type="text/javascript">
2. str = "tankZHang (231144)"+
3. "tank ying (155445)";
4. res = str.match(/tank/); //沒有加/g
5. alert(res); //顯示一個tank
6.
7. res = str.match(/tank/g); //加了/g
8. alert(res); //顯示為tank,tank
9. <strong></script></strong>
1. <script type="text/javascript">
2. str = "tankZHang (231144)"+
3. "tank ying (155445)";
4. res = str.match(/tank/); //沒有加/g
5. alert(res); //顯示一個tank
6.
7. res = str.match(/tank/g); //加了/g
8. alert(res); //顯示為tank,tank
9. <strong></script></strong>
10. <script type="text/javascript">
11. str = "tankZHang (231144)"+
12. "tank ying (155445)";
13. res = str.match(/tank/); //沒有加/g
14. alert(res); //顯示一個tank
15.
16. res = str.match(/tank/g); //加了/g
17. alert(res); //顯示為tank,tank
18. <strong></script></strong>
7.5 其他匹配
?=n ?=n 量詞匹配任何其后緊接指定字符串 n 的字符串。
//對其后緊跟 "all" 的 "is" 進行全局搜索:
var str="Is this all there is";
var patt1=/is(?= all)/g;//下面被標記的文本顯示了表達式獲得匹配的位置:Is this all there is
?!n 量詞匹配其后沒有緊接指定字符串 n 的任何字符串。
//對其后沒有緊跟 "all" 的 "is" 進行全局搜索:
var str="Is this all there is";
var patt1=/is(?! all)/g;//下面被標記的文本顯示了表達式獲得匹配的位置: Is this all there is
行首行尾
^..$
漢字,UTF-8編碼
[\u4e00-\u9fa5]
或字符:
var pattern1 = /google|baidu|bing/;//或模式匹配
alert(pattern1.test('this is baidu'))//true
alert(pattern1.test('soso'))//false
代替字符:
var pattern1 = /(.*)\s(.*)/;//替換
var str = 'google baidu';
alert(str.replace(pattern1, '$2 $1'));//baidu google
var pattern1 = /8(.*)8/;//獲取88之間的任意字符
var str = 'this is 8google8';
document.write(str.replace(pattern1, '<strong>$1</strong>'))
7.6 方法
search
stringObject.search(regexp) 方法用于檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。
返回值
stringObject 中第一個與 regexp 相匹配的子串的起始位置。
注釋:如果沒有找到任何匹配的子串,則返回 -1。
var str = "Visit W3School!"
document.write(str.search(/w3school/i))//結果6
replace
replace() 方法用于在字符串中用一些字符替換另一些字符,或替換一個與正則表達式匹配的子串。
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School")) //結果Visit W3School!
name = "Doe, John";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");//我們將把 "Doe, John" 轉換為 "John Doe" 的形式:
match()
var str="1 plus 2 equal 3"
document.write(str.match(/\d+/g)) //1,2,3
exec方法
方法用于檢索字符串中的正則表達式的匹配。
和match方法不同,不管正則表達式是否具有全局修飾符g,exec()都會返回一樣的數組,
第八章:window對象
8.1 計時器
setInterval
setTimeout
8.2 瀏覽器的定位和導航
window.location
顯示當前頁面的url
8.3 載入新的文檔
8.4 瀏覽歷史
history.forward();//前進
history.back();//后退
history.go(-2);//相當于單擊"后退"按鈕兩次
history.go(2);//相當于單擊"前進"按鈕1次
8.5 對話框
confirm alert prompt
8.6 打開和關閉窗口
第九章:腳本化文檔
9.1 dom概述
文檔對象模型(Document Object Model,簡稱DOM),是W3C組織推薦的處理可擴展標志語言的標準編程接口。在網頁上,組織頁面(或文檔)的對象被組織在一個樹形結構中,用來表示文檔中對象的標準模型就稱為DOM
9.2 選取文檔元素
document.getElementById("d1");//通過id找
document.getElementsByName("a1");//通過name屬性找
document.getElementsByTagName("p")[0];//通過標簽來找
document.getElementsByClassName("c1");//通過class名稱來找
9.3 文檔結構和遍歷
document.getElementById("item1").parentNode; //返回父節點
document.body.childNodes;//子節點集合
document.body.firstChild;//第一個子節點
document.body.lastChild;//最后一個子節點,如果沒有子節點返回null
document.getElementById("item1").nextSibling();//后面的那個兄弟節點
document.getElementById("item1").previousSibling();//前面的那個兄弟節點
document.getElementsByTagName("BUTTON")[0].childNodes[0].nodeValue;//nodeValue節點值
document.body.nodeName;//獲得 body 元素的節點名稱
9.4 屬性
9.5 元素的內容
innerHTML 設置或獲取位于對象起始和結束標簽內的 HTML
innerText 設置或獲取位于對象起始和結束標簽內的文本
outerHTML 設置或獲取對象及其內容的HTML形式
outerText 設置(包括標簽)或獲取(不包括標簽)對象的文本
var innerHTML = document.getElementById("mydiv").innerHTML;
var innerText = document.getElementById("mydiv").innerText; //Firefox不支持
var outerHTML = document.getElementById("mydiv").outerHTML;
var outerText = document.getElementById("mydiv").outerText; //Firefox不支持
console.info(innerHTML); //輸出<span style="color:red">span1</span> span2
console.info(innerText); //輸出span1 span2
console.info(outerHTML); //輸出<div id="mydiv"><span style="color:red">span1</span>span2</div>
console.info(outerText); //輸出span1 span2
PS: innerHTML是符合W3C標準的屬性,而innerText對Firefox是不支持的,因此,盡可能地去使用innerHTML,
而少用innerText,如果要輸出不含HTML標簽的內容,可以使用innerHTML取得包含HTML標簽的內容后,
再用正則表達式去除HTML標簽,下面是一個簡單的符合W3C標準的示例:
console.info(document.getElementById('mydiv').innerHTML.replace(/<.+?>/gim,''));
9.6 創建、插入和刪除節點
9.6.1 創建節點
var newnode=document.createTextNode("hello world");
9.6.2 插入節點
9.6.3 刪除和替換節點
window.onload = function () {
var oInp = document.getElementById('inp');
var oBtn = document.getElementById('btn');
var oUl1 = document.getElementById('ul1');
var oP = document.getElementById('p1');
var oBtn1 = document.getElementById('btn1');
var oDiv = document.getElementById('div1');
oBtn.onclick = function () {
var oLi = document.createElement('li'); //動態創建li元素
oLi.innerHTML = oInp.value;
//oUl1.appendChild(oLi); // 追加子元素
//在指定的元素之前插入新元素
if (oUl1.children[0]) { //兼容IE
oUl1.insertBefore(oLi, oUl1.children[0]);
} else {
oUl1.appendChild(oLi)
}
var oA = document.createElement('a');
oA.innerHTML = '刪除';
oA.href = 'javascript:;';
oA.onclick = function () {
oUl1.removeChild(this.parentNode); //刪除節點 只能在父級下進行操作
}
oLi.appendChild(oA);
}
oBtn1.onclick = function () {
// document.body.replaceChild(oP,oDiv); //p標簽替換div
oDiv.appendChild(oP); // 也可以操作已有的節點 把p標簽添加到div里面
}
}
<input type="text" id="inp" />
<input type="button" value="添加" id="btn" />
<ul id="ul1"></ul>
<hr/>
<p id="p1">我是P標簽</p>
<button id="btn1">替換</button>
<div id="div1">我是div</div>
第十章:Canvas
10.1 創建畫圖環境、畫矩形、劃線
window.onload = function () {
var oC = document.getElementById("c1");
var oGC = oC.getContext("2d");//繪圖環境
//demo1:繪制實心方塊,left/top/width/height 默認是黑色
//oGC.fillRect(100,50,200,100);
//demo2:帶邊框的空心方塊,邊框默認為2px,要讓它默認為1px,left,top多加0.5
//oGC.strokeRect(100,50,200,100);
//demo3:oGC.fillStyle="#f00";//填充顏色
//oGC.fillRect(100,50,200,100);
// oGC.strokeStyle="#00f";
// oGC.lineWidth=10;
// oGC.strokeRect(100,50,200,100);
//demo4:邊界繪制
//oGC.lineJoin = "round";//圓角
// oGC.lineJoin = "bevel";//斜角
// oGC.lineWidth=10;
// oGC.strokeRect(100,50,200,100);
//demo5:繪制路徑
// oGC.beginPath();
// oGC.moveTo(100,100);
// oGC.lineTo(200,200);
// oGC.lineTo(300,200);
// oGC.closePath();//閉合
// oGC.stroke();//默認是黑色的
// oGC.beginPath();
// oGC.moveTo(300, 300);
// oGC.lineTo(200, 200);
// oGC.lineTo(300, 200);
// oGC.closePath();//閉合
// oGC.fill();//填充
//demo6:繪制矩形
// oGC.rect(20, 20, 150, 100);
//oGC.fill();//填充
// oGC.stroke();//畫邊框
// oGC.clearRect(0,0,oC.width,oC.height);//清除
//demo7:保存狀態
//沒有save和restore那么oGC.fillStyle = "red";對兩個都有作用,有了后,只對第一個有作用,第二個沒有作用
// oGC.save();//保存路徑
// oGC.fillStyle = "red";
// oGC.beginPath();
// oGC.moveTo(100, 100);
// oGC.lineTo(200, 200);
// oGC.lineTo(300, 200);
// oGC.closePath();
// oGC.fill();
// oGC.restore();//恢復路徑
// oGC.beginPath();
// oGC.moveTo(300, 300);
// oGC.lineTo(400, 400);
// oGC.lineTo(500, 600);
// oGC.closePath();
// oGC.fill();
//demo8:端點樣式
// oGC.lineWidth=20;
// oGC.lineCap="round";//圓角,如果設置為square則高度多出為寬一半的值
// oGC.moveTo(100,100);
// oGC.lineTo(200,200);
// oGC.stroke();
}
10.2 畫圓
var oC = document.getElementById("c1");
var oGC = oC.getContext("2d");//繪圖環境
//demo1:繪制圓形
//oGC.moveTo(200,200);
//參數:x,y起始位置,半徑,起始弧度(角度*Math.PI/180), 旋轉方向
// oGC.arc(200,200,150,0,90*Math.PI/180,false);//0度在圓的最右邊,最上面為-90度,最下面為正90度
// oGC.stroke();
//demo2:畫鐘表
function toDraw() {
var oDate = new Date();
var oHours = oDate.getHours();
var oMin = oDate.getMinutes();
var oSen = oDate.getSeconds();
var oHoursValue = (-90 + oHours * 30 + oMin / 2) * Math.PI / 180;
var oMinValue = (-90 + oMin * 6) * Math.PI / 180;
var oSenValue = (-90 + oSen * 6) * Math.PI / 180;
var x = 200;
var y = 200;
var r = 150;
oGC.clearRect(0, 0, oC.width, oC.height);
oGC.moveTo(x, y);
oGC.arc(x, y, r, 0, 6 * Math.PI / 180, false);
oGC.beginPath();
for (var i = 0; i < 60; i++) {
oGC.moveTo(x, y);
oGC.arc(x, y, r, 6 * i * Math.PI / 180, 6 * (i + 1) * Math.PI / 180, false);
}
oGC.closePath();
oGC.stroke();
oGC.fillStyle = "#fff";
oGC.beginPath();
oGC.moveTo(x, y);
oGC.arc(x, y, r * 19 / 20, 0, 360 * Math.PI / 180, false);
oGC.closePath();
oGC.fill();
oGC.lineWidth = 3;
oGC.beginPath();
for (var i = 0; i < 12; i++) {
oGC.moveTo(x, y);
oGC.arc(x, y, r, 30 * i * Math.PI / 180, 30 * (i + 1) * Math.PI / 180, false);
}
oGC.closePath();
oGC.stroke();
oGC.fillStyle = "#fff";
oGC.beginPath();
oGC.moveTo(x, y);
oGC.arc(x, y, r * 18 / 20, 0, 360 * Math.PI / 180, false);
oGC.closePath();
oGC.fill();
oGC.lineWidth = 5;
oGC.beginPath();
oGC.moveTo(x, y);
oGC.arc(x, y, r * 8 / 20, oHoursValue, oHoursValue, false);
oGC.closePath();
oGC.stroke();
oGC.lineWidth = 3;
oGC.beginPath();
oGC.moveTo(x, y);
oGC.arc(x, y, r * 13 / 20, oMinValue, oMinValue, false);
oGC.closePath();
oGC.stroke();
oGC.lineWidth = 1;
oGC.beginPath();
oGC.moveTo(x, y);
oGC.arc(x, y, r * 17 / 20, oSenValue, oSenValue, false);
oGC.closePath();
oGC.stroke();
}
setInterval(toDraw, 1000);
toDraw();
10.3 顏色、樣式、陰影、漸變
window.onload = function () {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
//demo1:填充矩形
//ctx.fillStyle = "#0f0";//
//ctx.fillRect(20, 20, 100, 100);
//demo2:矩形框
//ctx.strokeStyle = "#0000ff";
//ctx.strokeRect(20, 20, 150, 100);
//demo3 陰影
//ctx.shadowBlur = 20;//模糊級數是 20
//ctx.shadowOffsetX = 20;//帶有向右偏移 20 像素的陰影
//ctx.shadowOffsetY = 20;//帶有向下偏移 20 像素的陰影
//ctx.shadowColor = "black";
//ctx.fillStyle = "blue";
//ctx.fillRect(20, 20, 100, 80);
//demo4:線性漸變
//var grd = ctx.createLinearGradient(0, 0, 170, 0);//漸變開始點的 x 坐標,漸變開始點的 y 坐標,漸變結束點的 x 坐標,漸變結束點的 y 坐標
//grd.addColorStop(0, "black");
//grd.addColorStop(0.5, "red");
//grd.addColorStop(1, "white");
//ctx.fillStyle = grd;
//ctx.fillRect(20, 20, 150, 100);
//demo5:圖片重復
//var img = document.getElementById("lamp");
//var pat = ctx.createPattern(img, "repeat");//repeat默認,repeat-x,repeat-y,no-repeat
//ctx.rect(0, 0, 150, 100);
//ctx.fillStyle = pat;
//ctx.fill();
//demo6:徑向漸變
//var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);//漸變的開始圓的 x 坐標,漸變的開始圓的 y 坐標,開始圓的半徑, 漸變的結束圓的 x 坐標,漸變的結束圓的 y 坐標,結束圓的半徑
//grd.addColorStop(0, "red");
//grd.addColorStop(1, "white");
//ctx.fillStyle = grd;
//ctx.fillRect(10, 10, 150, 100);
10.4 路徑方法
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
//demo1:剪切
//ctx.rect(50, 20, 200, 120);
//ctx.stroke();
//ctx.clip();
//// 在 clip() 之后繪制綠色矩形
//ctx.fillStyle = "green";
//ctx.fillRect(0, 0, 150, 100);
//demo2:繪制一條二次貝塞爾曲線
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.quadraticCurveTo(20, 100, 200, 20);//貝塞爾控制點的 x 坐標,貝塞爾控制點的 y 坐標,結束點的 x 坐標,結束點的 y 坐標
ctx.stroke();
//demo3:繪制一條三次貝塞爾曲線
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.bezierCurveTo(20, 100, 200, 100, 200, 20);//第一個貝塞爾控制點的 x 坐標, 第一個貝塞爾控制點的 y 坐標,第二個貝塞爾控制點的 x 坐標,第二個貝塞爾控制點的 y 坐標,結束點的 x 坐標,結束點的 y 坐標
ctx.stroke();
//在畫布上創建介于兩個切線之間的?。?/span>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.beginPath();
ctx.moveTo(20, 20); // 創建開始點
ctx.lineTo(100, 20); // 創建水平線
ctx.arcTo(150, 20, 150, 70, 50); // 創建弧, 弧的起點的 x 坐標, 弧的起點的 y 坐標, 弧的終點的 x 坐標, 弧的終點的 y 坐標, 弧的半徑
ctx.lineTo(150, 120); // 創建垂直線
ctx.stroke(); // 進行繪制
//繪制一個矩形,如果點 20,50 位于當前路徑中:
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.rect(20, 20, 150, 100);
if (ctx.isPointInPath(20, 50)) {
ctx.stroke();
};
10.5 轉換
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
//demo1:放大或者縮小
//ctx.strokeRect(5, 5, 25, 15);
//ctx.scale(2, 2);//繪制矩形,放大到 200%,然后再次繪制矩形
//ctx.strokeRect(5, 5, 25, 15);
//demo2:旋轉
//ctx.rotate(20 * Math.PI / 180);//旋轉角度,以弧度計,如需將角度轉換為弧度,請使用 degrees*Math.PI/180 公式進行計算。
//ctx.fillRect(50, 20, 100, 50);
//demo3:位移
//ctx.fillRect(10, 10, 100, 50);
//ctx.translate(70, 70);//添加到水平坐標(x)上的值,添加到垂直坐標(y)上的值
//ctx.fillRect(10, 10, 100, 50);
//demo4:替換繪圖的當前轉換矩陣
ctx.fillStyle = "yellow";
ctx.fillRect(0, 0, 250, 100)
ctx.transform(1, 0.5, -0.5, 1, 30, 10);//transform() 允許縮放、旋轉、移動并傾斜當前的環境。
//水平縮放繪圖,水平傾斜繪圖,垂直傾斜繪圖,垂直縮放繪圖,水平移動繪圖,垂直移動繪圖
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 250, 100);
ctx.transform(1, 0.5, -0.5, 1, 30, 10);
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, 250, 100);
10.6 文本
ctx.font = "40px Arial";
ctx.fillText("Hello World", 10, 50);
ctx.strokeText("Hello World!", 10, 50);
10.7 圖像/視頻繪制
10.7.1 圖片:
var img = document.getElementById("ig");
ctx.drawImage(img, 30, 10, 200, 100);//圖片對象,開始剪切的 x 坐標位置,開始剪切的 y 坐標位置,被剪切圖像的寬度,被剪切圖像的高度
10.7.2 視頻:
var v = document.getElementById("video1");
v.addEventListener("play", function () {
var i = setInterval(function () {
ctx.drawImage(v, 0, 0, 270, 135);
}, 20);
});
v.addEventListener('pause', function () { window.clearInterval(i); });
v.addEventListener('ended', function () { clearInterval(i); });
10.7.3 創建新的、空白的 ImageData 對象
var imgData = ctx.createImageData(100, 100);
for (var i = 0; i < imgData.data.length; i += 4) {
imgData.data[i + 0] = 255;//red
imgData.data[i + 1] = 0;//green
imgData.data[i + 2] = 0;//blue
imgData.data[i + 3] = 255;//alpha
}
ctx.putImageData(imgData, 10, 10);
10.7.4 getImageData() 方法返回 ImageData 對象,該對象拷貝了畫布指定矩形的像素數據
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 50, 50);
function copy() {
var imgData = ctx.getImageData(10, 10, 50, 50);//通過 getImageData() 復制畫布上指定矩形的像素數據,然后通過 putImageData() 將圖像數據放回畫布
ctx.putImageData(imgData, 10, 70);
}
copy();
10.8 合成
10.8.1 globalAlpha
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 75, 50);
// 調節透明度
ctx.globalAlpha = 0.2;
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 75, 50);
ctx.fillStyle = "green";
ctx.fillRect(80, 80, 75, 50);
10.8.2 globalCompositeOperation
ctx.fillStyle = "red";
ctx.fillRect(20, 20, 75, 50);
ctx.globalCompositeOperation = "source-over"; //藍色在上面
ctx.fillStyle = "blue";
ctx.fillRect(50, 50, 75, 50);
ctx.fillStyle = "red";
ctx.fillRect(150, 20, 75, 50);
ctx.globalCompositeOperation = "destination-over";//紅色在上面
ctx.fillStyle = "blue";
ctx.fillRect(180, 50, 75, 50);
本文由站長原創或收集,不代表本站立場,如若轉載,請注明出處:http://www.exceltransport.net/post/57.html
共有 1 條評論
gBqsPxAZ
1