從宏觀角度來(lái)看瀏覽器,并對(duì)一下API進(jìn)行總結(jié):
建議參考的資料-比較直觀的API
官方API文檔
一琳省、引言
瀏覽器為開發(fā)者提供了一系列API迎吵,根據(jù)不同的能力分成不同的模塊:
1躲撰、瀏覽器操作(BOM)
2、頁(yè)面操作 (DOM)
3击费、網(wǎng)絡(luò)操作 (websocket等)
二茴肥、瀏覽器操作(BOM)
對(duì)瀏覽器的操作API 即 window ,代表當(dāng)前這個(gè)瀏覽器窗口荡灾,window及其子對(duì)象組成瀏覽器對(duì)象模型瓤狐。
常用屬性:(1-7對(duì)象屬性需要后面展開)
1..document --指向DOM
2..frames --返回頁(yè)面中所有框架數(shù)據(jù)集合
3..history --用戶訪問(wèn)過(guò)的 URL集合
4..location --當(dāng)前 URL 的信息對(duì)象
5..navigator --有關(guān)瀏覽器的信息對(duì)象
6..screen --有關(guān)客戶端顯示屏幕的信息對(duì)象
7..closed --返回窗口是否已關(guān)閉
8..innerHeight --頁(yè)面顯示范圍的高度 ie678不兼容 代替:
document.documentElement.clientHeight
9..innerWidth --頁(yè)面顯示范圍的寬度 ie678不兼容 代替:
document.documentElement.clientWidth
10..localStorage --本地存儲(chǔ)長(zhǎng)期有效
11..sessionStorage --本地存儲(chǔ)關(guān)閉窗口消失
12..length --窗口框架數(shù)量
13..pageXOffset --設(shè)置或返回當(dāng)前頁(yè)面相對(duì)于窗口顯示區(qū)左上角的 X 位置
14..pageYOffset --設(shè)置或返回當(dāng)前頁(yè)面相對(duì)于窗口顯示區(qū)左上角的 Y 位置
15..devicePixelRatio --物理像素和 CSS 像素單位的倍率關(guān)系,Retina 屏為2批幌,有些安卓屏為3
常用方法:
1.提示類:alert()础锐、 confirm()、 prompt()荧缘、 print()
2.頁(yè)面操作類:close()皆警、open()、stop()截粗、focus()信姓、moveBy()、moveTo()绸罗、scrollBy()意推、scrollTo()
3.頁(yè)面內(nèi)容操作類:getComputedStyle(element)-返回指定元素CSS樣式、setInterval()珊蟀、clearInterval()菊值、setTimeout()、clearTimeout()
三育灸、頁(yè)面操作(DOM)
在window 對(duì)象中包含DOM文檔的窗口腻窒,DOM會(huì)對(duì)當(dāng)前頁(yè)面進(jìn)行操作,即文檔對(duì)象模型(document)
基礎(chǔ)操作:
增:
1..createElement() --創(chuàng)建元素節(jié)點(diǎn)
2..createTextNode() --創(chuàng)建文本節(jié)點(diǎn)
3..createComment() --創(chuàng)建注釋節(jié)點(diǎn)
4..createDocumentType() --創(chuàng)建<!DOCTYPE html>
5..createDocumentFragment() --創(chuàng)建空的 DocumentFragment 對(duì)象(相當(dāng)于空的div)磅崭,并返回此對(duì)象儿子,,用來(lái)減少DOM刷新次數(shù)
6..createProcessingInstruction() --<? a 1?>
7..createCDATASection() --創(chuàng)建時(shí)間節(jié)點(diǎn)砸喻,xml
8.當(dāng)前節(jié)點(diǎn).appendChild() --將參數(shù)節(jié)點(diǎn)移動(dòng)到當(dāng)前節(jié)點(diǎn)內(nèi)部后面
9.當(dāng)前節(jié)點(diǎn).insertBefore() --將參數(shù)節(jié)點(diǎn)移動(dòng)到當(dāng)前節(jié)點(diǎn)內(nèi)部前面
刪:
1.當(dāng)前節(jié)點(diǎn).removeChild() --刪除指定子節(jié)點(diǎn)
改:
1.當(dāng)前節(jié)點(diǎn).replaceChild() --替換子節(jié)點(diǎn)
查:
1.當(dāng)前節(jié)點(diǎn).parentNode --返回父節(jié)點(diǎn)
2.當(dāng)前節(jié)點(diǎn).children --返回子節(jié)點(diǎn)集合
3.當(dāng)前節(jié)點(diǎn).childNodes --返回子節(jié)點(diǎn)集合包括文本節(jié)點(diǎn)柔逼,有兼容性
4.當(dāng)前節(jié)點(diǎn).firstChild --第一個(gè)子節(jié)點(diǎn)
當(dāng)前節(jié)點(diǎn).firstElementChild ie9+ 兼容
5.當(dāng)前節(jié)點(diǎn).lastChild --最后一個(gè)子節(jié)點(diǎn)
當(dāng)前節(jié)點(diǎn).lastElementChild ie9+ 兼容
6.當(dāng)前節(jié)點(diǎn).nextSibling --下一個(gè)兄弟節(jié)點(diǎn)
當(dāng)前節(jié)點(diǎn).nextElementSibling ie9+ 兼容
7.當(dāng)前節(jié)點(diǎn).previousSibling --上一個(gè)兄弟節(jié)點(diǎn)
當(dāng)前節(jié)點(diǎn).previousElementSibling ie9+ 兼容
8..getElementsByName()
9..getElementById()
10..getElementsByClassName()
11..getEelmentsByTagName()
其他:
1.當(dāng)前節(jié)點(diǎn).cloneNode(true) --深拷貝
2.當(dāng)前節(jié)點(diǎn).cloneNode(false) --淺拷貝
3.當(dāng)前節(jié)點(diǎn).hasChildNodes() --判斷是否有子節(jié)點(diǎn)
4.當(dāng)前節(jié)點(diǎn).contains() --判斷是否包含某節(jié)點(diǎn)
5.當(dāng)前節(jié)點(diǎn).compareDocumentPosition() --比較兩節(jié)點(diǎn)關(guān)系
6.當(dāng)前節(jié)點(diǎn).contentEditable() --是否可編輯
7.當(dāng)前節(jié)點(diǎn).isEqualNode() --兩節(jié)點(diǎn)是否完全相同
9.當(dāng)前節(jié)點(diǎn).isSameNode() --檢查時(shí)是否為統(tǒng)一節(jié)點(diǎn)
元素屬性:
1.當(dāng)前節(jié)點(diǎn).getAttribute()
2.當(dāng)前節(jié)點(diǎn).setAttribute()
3.當(dāng)前節(jié)點(diǎn).removeAttribute()
4.當(dāng)前節(jié)點(diǎn).hasAttribute()
注:
- 1、若將屬性當(dāng)做節(jié)點(diǎn)來(lái)看恩够,還可以使用setAttributeNode()getAttributeNode()
- 2卒落、內(nèi)置屬性可以像property 一樣進(jìn)行訪問(wèn),dom.attributes.屬性
用戶的自定義屬性不可以使用dom.attributes.屬性獲取蜂桶,只能使用上面的方式儡毕。
樣式設(shè)置:
1.當(dāng)前節(jié)點(diǎn).style.backgroundColor="#ffffff"
2.當(dāng)前節(jié)點(diǎn).style.cssText="background-color:#ffffff;font-size:24px;"
3.當(dāng)前節(jié)點(diǎn).className="a";
文本節(jié)點(diǎn)設(shè)置:
1.innerText 低版本Firefox不支持
2.innerHTML
3.textContent ie678不支持
四、網(wǎng)絡(luò)操作
1.http
2.WebSocket