<!-- DOM樹:二叉樹 --> /* <?xml version="1.0" encoding="UTF-8"> // 告訴瀏覽器以哪種類型進行解析 <node> <child /> </node> */ <!DOCTYPE html> // 以html類型進行解析文檔內容 <html> <body> </body> </html>
parentNode :每一個節點都有一個parentNode屬性,它表示元素的父節點。Element的父節點多是Element,Document或DocumentFragment;
parentElement :返回元素的父元素節點,與parentNode的區別在於,其父節點必須是一個Element元素,若是不是,則返回null;html
children :返回一個實時的 HTMLCollection ,子節點都是Element,IE9如下瀏覽器不支持;node
childNodes :返回一個實時的 NodeList ,表示元素的子節點列表,注意子節點可能包含文本節點、註釋節點等;面試
firstChild :返回第一個子節點,不存在返回null,與之相對應的還有一個 firstElementChild ;ajax
lastChild :返回最後一個子節點,不存在返回null,與之相對應的還有一個 lastElementChild ;express
previousSibling :節點的前一個節點,若是不存在則返回null。注意有可能拿到的節點是文本節點或註釋節點,與預期的不符,要進行處理一下。json
nextSibling :節點的後一個節點,若是不存在則返回null。注意有可能拿到的節點是文本節點,與預期的不符,要進行處理一下。後端
previousElementSibling :返回前一個元素節點,前一個節點必須是Element,注意IE9如下瀏覽器不支持。api
nextElementSibling :返回後一個元素節點,後一個節點必須是Element,注意IE9如下瀏覽器不支持。跨域
一、setAttribute 給元素設置屬性:
二、getAttribute
三、hasAttribute瀏覽器
elem.style.color = 'red'; elem.style.setProperty('font-size', '16px'); elem.style.removeProperty('color');
var style = document.createElement('style'); style.innerHTML = 'body{color:red} #top:hover{background-color: red;color: white;}'; document.head.appendChild(style);
三、classList獲取樣式屬性
[!NOTE]
瞭解dom節點樣式(classList)的remove, add, toggle, contains, replace等方法的使用。
四、window.getComputedStyle
經過 element.sytle.xxx 只能獲取到內聯樣式,藉助 window.getComputedStyle 能夠獲取應用到元素上的全部樣式,IE8或更低版本不支持此方法。
var style = window.getComputedStyle(element[, pseudoElt]);
// proprty------>>>獲取nodeName和nodeType(property實際上就是JS對象的一個屬性)如:text : 3(#text), p : 1(p) console.log(pList[0].nodeName); // p console.log(pList[0].nodeType); // 1 // attribute------>>> 獲取一個HTML標籤的屬性信息(設置和修改) var a = document.getElementsByTagName('a')[0]; console.log(a.getAttribute('data-origin')); console.log(a.getAttribute("href_name")); // 能夠給一個HTML標籤設置任意的屬性名稱,不管這個屬性內部是否存在 a. setAttribute('sex', 'male') // 區別: // (JS對象&HTML標籤)property其實是一個普通JS對象自己的基本屬性,可是attribute其實是一個HTML標籤上面的屬性信息
[!NOTE]
區別:(JS對象&HTML標籤)property其實是一個普通JS對象自己的基本屬性,可是attribute其實是一個HTML標籤上面的屬性信息
var ua = navigator.userAgent; var isChrome = ua.indexOf('Chrome'); console.log('is Chrome?', isChrome > 0, navigator.userAgent); console.log('電腦屏幕大小:', screen.width, screen.height)
// location console.log(location.href); // 完整的url地址,http://localhost:8080/JS-Professional/begin/02-%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6/01-%E4%BB%8E%E5%9F%BA%E7%A1%80%E5%88%B0%E8%BF%9B%E9%98%B6.html?_ijt=i8ctmkc87dvh03tdaf51rn5v1i console.log(location.protocol) // http/https console.log(location.host, location.hostname) // www.52tech.tech console.log(location.pathname); // host以後的所有內容,JS-Professional/begin/02-%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6/01-%E4%BB%8E%E5%9F%BA%E7%A1%80%E5%88%B0%E8%BF%9B%E9%98%B6.html console.log(location.search); // search就是?以後的所有內容,?_ijt=i8ctmkc87dvh03tdaf51rn5v1i(包括?) console.log(location.hash); // #後面的內容(包括#)
function bindEvent(ele, type, selector, fn){ if (fn == null) { fn = selector; selector = null; } // addEventListener 的最後一個參數默認是false, 表示的是事件冒泡,自下向上去捕獲事件,true:表示事件捕獲,表示事件自外向裏的方式 ele.addEventListener(type, function(e){ if (selector) { // 使用的是代理的方式的話 if (e.target.matches(selector)){ // 這裏須要去判斷當前點擊的那個對象是否是我點擊的那個對象 fn.call(e.target, e); } } else { // 不使用代理的話 fn(e); } }); } // 這裏表示對這個div1 內部的全部的a標籤使用事件冒泡 bindEvent(document.getElementById('div4'), 'click', 'p', function (e) { console.log(this.innerHTML + 'hahaah') }) 版本2: /** * 實現一個通用的事件綁定函數(代碼簡潔,減小了瀏覽器的佔用) * @param element * @param type * @param selector * @param fn */ function bindEvent(element, type, selector, fn) { // if (fn === null || fn === undefined) // 這裏能夠直接優化爲fn == null if (fn == null){ // 只有3個參數的話 fn = selector; selector = null; } element.addEventListener(type, function (e) { var target; // 若是selector有的話,說明此事element就是一個代理 if (selector) { target = e.target; // 若是當前的target知足這個選擇器的話 // 若是元素被指定的選擇器字符串選擇,Element.matches() 方法返回true; 不然返回false。 // document.getElementById('div1').matches('div') true // matches裏面的參數其實是一個HTML標籤:a, p, div …………, 內容也是不區分大小寫的,有點相似於nodeName 或者nodeType 這樣的判斷 if (target.matches(selector)){ // 修改this的指向 fn.call(target, e); } }else { fn(e); } }); }
[!NOTE]
"DOM2級事件」規定的事件流包含三個階段:事件捕獲階段,處於目標階段和事件冒泡階段。首先發生的是事件捕獲,而後是實際的目標接收到事件,最後階段是冒泡階段。
- 事件冒泡:
按照DOM樹形結構向上冒泡(p --- >>> div --- >>> body --- >>> document),- 事件捕獲
(document—>—>—>的順序進行傳播的)
// 代理(div3裏面的全部a標籤都須要進行事件的監聽) var div3 = document.getElementById('div3'); bindEvent(div3, 'click', function (e) { e.preventDefault(); e.stopPropagation() // 能夠獲取真實觸發的那個元素 var target = e.target; console.log(target.nodeName, target.nodeType); // 每一種頁面標籤實際上都有一個本身專屬的nodeName // 這裏的目的主要是用於過濾,只處理a標籤的事件處理 if ('A' === target.nodeName) { // 若是當前點擊的a標籤就是本身 alert(target.innerHTML) } })
[!NOTE]
跨域:瀏覽器有同源策略,不容許ajax訪問其餘域接口
- 跨域條件:協議、域名、端口,有一個不一樣就是跨域
能夠跨域加載資源的3個標籤:
<img src="">, <link href="">, <script src="">
//封裝一個jsonp請求的函數 function query(opt) { let str = "" for (let key in opt) { str += key + "=" + opt[key] + "&" } return str } //設置默認回調函數的名字 const defaultOptions = { callbackName: "callback" } function jsonp(url, opt, options = defaultOptions) { //參數解析 URL爲訪問的接口 opt爲傳播的數據 option 爲接受參數的回調函數 return new Promise((resolve, reject) => { //判斷下這個?是否是存在 let index = url.indexOf("?"); url += index != -1 ? query(opt) : "?" + query(opt); url = url + `${options.callbackName}=${options.callbackName}`; //首先創造一個標籤 帶有src的 const scriptDom = document.createElement("script"); //設置其src屬性 scriptDom.setAttribute("src", url); //在window系統上建立一個回調函數用來接受數據 window[options.callbackName] = (res) => { //在接受到了參數動態刪除這個script節點和window上面的方法 delete window[options.callbackName]; document.body.removeChild(scriptDom) //接受成功後調用resolve if (res) { resolve(res) } else { reject("服務器暫沒有獲取到數據") } } //動態建立script標記,錯誤的監聽 scriptDom.addEventListener('error', () => { delete window['jsonpCallback']; document.body.removeChild(script); reject('服務器加載失敗!'); }); document.body.append(scriptDom) }) }
const url = require("url") router.get("/api", (req, res, next) => { //將script標籤的src的URL請求轉成對象 const opj = url.parse(req.url, true).query; //而後原理就是調用這個回調函數來進行傳參 let { callback } = opj; //若是這個回調函數存在證實是jsonp請求 if (callback) { let resault = JSON.stringify({ code: 1, msg: "express框架傳回去的參數" }); res.send(`${callback}(${resault})`) } })
sessionStorage:會話級別
[!WARNING] 【坑】IOS safari的隱藏模式下,使用localStorage.getItem('key') 會報錯,建議使用try{}catch(e){} 來實現能夠防止報錯