
批改狀態(tài):合格
老師批語:dom與事件, 在全棧開發(fā)中的占有重要地位
1、節(jié)點時DOM樹種的最小單元:節(jié)點是有類型的:元素、屬性、文本、文檔、注釋、片段等
document:(window.document)獲取文檔;定義的快速入口:
document.doctype:獲得文檔類型
document.documentElement:獲取文檔種的根節(jié)點
document.head:獲取head節(jié)點
document.body:獲取body節(jié)點
document.title:獲取head中的title節(jié)點
document.links:獲取a鏈接的節(jié)點集合;
2、DOM操作選擇函數(shù)
document.getElementsByTagName("元素標簽名"); //返回HTMLCollection集合
document.getElementById("ID值"); //返回html代碼(元素)
document.getElementsByName("name屬性值"); //返回Nodelist集合,節(jié)點也可以通過數(shù)組和item()方法訪問
document.getElementsByClassName("class屬性值"); //返回HTMLCollection集合
document.querySelector("Css選擇器"); //返回html代碼
document.querySelectorAll("css選擇器"); //返回Nodelist集合
3、屬性操作:
.innerText:操作元素中的文字
.innerHTML:可以操作元素中的文字且可以添加html代碼
.style.css屬性:可以操作css屬性值
4、HTMLCollection(HTML元素)集合和Nodelist(節(jié)點)集合可以通過for()
循環(huán)遍歷,Nodeslist可以通過forEach()遍歷,而HTMLCollection不可以通過forEach
()遍歷,可以通過Array.from()
把HTMLCollection轉(zhuǎn)換成數(shù)組來遍歷
5、HTMLCollection(HTML元素)集合和Nodelist(節(jié)點)集合都可以通過數(shù)組方式訪問;也可以通過item()
方法訪問,兩都有length()
方法獲取長度(數(shù)量);
6、節(jié)點集合操作函數(shù):
節(jié)點集合.childNodes; //返回包含所有節(jié)點類型的集合
節(jié)點集合.children; //只包含元素節(jié)點類型的集合
節(jié)點集合.childElementCount; //統(tǒng)計元素節(jié)點的數(shù)量
節(jié)點集合.childNodes.length; //統(tǒng)計所有類型節(jié)點的數(shù)量
7、節(jié)點操作函數(shù):
節(jié)點.nodeName; //返回元素標簽
節(jié)點.nodeType; //返回元素類型
節(jié)點.nodeValue; //返回元素值
節(jié)點集合.firstChild; //返回第一個節(jié)點
節(jié)點集合.firstElementChild; //返回第一個元素節(jié)點
節(jié)點集合.lastChild; //返回最后一個節(jié)點
節(jié)點集合.lastElementChild; //返回最后一個元素節(jié)點
節(jié)點.previousSibling; //返回當前節(jié)點的前一個節(jié)點
節(jié)點.nextSibling; //返回當前節(jié)點的前一個節(jié)點
節(jié)點.previousElementSibling; //返回當前節(jié)點的前一個元素節(jié)點
節(jié)點.nextElementSibling; //返回當前節(jié)點的前一個元素節(jié)點
節(jié)點.parentElement;//返回當前節(jié)點的父級元素;
8、DOM屬性操作,在JS中class是關(guān)鍵字所以使用className代替class屬性值,但由于class屬性操作經(jīng)常被使用,classList對象來操作class屬性值,更方便
classList對象中的方法:
classList.add()增加值
classList.remove()移除值
classList.toggle()判斷,有此值則移除,無則添加
classList.replace()替換
9、在操作HMTL自定義的屬性(例如:data-index)時,常用dataset對象來獲取其屬性值(例如:dataset.index);如果自定義屬性是由兩個單詞通過-
連接組成時;在獲取其屬性值,把-
去掉,把第二個單詞字母大寫即可(例如:data-index-num,獲?。篸ataset.indexNum)
10、append()
和appendChild()
添加元素和節(jié)點;
11、在某個元素中子元素之前插入元素:
parentElement.insertBefore(element,element);
在某個元素執(zhí)行插入操作,第一個元素為要插入的元素,第二個參數(shù)為以那個元素為參照
1、addEventListener('事件',function(){},false);
事件監(jiān)聽函數(shù),最后一個值false時為冒泡事件,為true為捕獲事件
2、常見的事件:
(1)輸入文本時:
onchange內(nèi)容改變事件
onfocus獲得焦點事件
onblur失去焦點事件
onkeydown鍵盤按鍵按下事件
onkeypress釋放鍵盤按鍵事件
(2)鼠標事件
onclick點擊事件
ondbclick表示鼠標快速點擊了兩次。
mouseover表示鼠標經(jīng)過。
mouseout表示鼠標離開區(qū)域
3、new Event('事件')
創(chuàng)建事件對象,dispatchEvent('事件對象');
委派事件對象來觸發(fā)元素事件;
4、回調(diào)函數(shù)中事件對象和類型:
ev.type; //監(jiān)聽事件類型
ev.targe; //觸發(fā)事件元素
ev.currentTarget; //綁定事件元素
5、事件委托把子級事件委托父級來觸發(fā),主要通過觸犯事件的元素(ev.targe
)經(jīng)行區(qū)分是那個子級元素觸發(fā)的,而非幫綁定事件的元素;
6、冒泡和捕獲事件的區(qū)別:
1.demo
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>獲取dom元素</title>
</head>
<body>
<ul id="list">
<li class="item" name="first">item1</li>
<li class="item">item2</li>
<li class="item">item3</li>
<li class="item active">item4</li>
<li class="item">item5</li>
<a href="#"></a>
</ul>
</body>
</html>
<script>
var cl = console.log.bind(console);
//標簽
var lis = document.getElementsByTagName("li"); //返回HTMLCollection集合
cl(lis);
//ID
var ul = document.getElementById("list"); //返回html代碼(元素)
cl(ul);
ul.style.fontSize = "18px";
//name
var li1 = document.getElementsByName("first"); //返回Nodelist集合,節(jié)點也可以通過數(shù)組和item()方法訪問
cl(li1[0]);
li1.item(0).innerText = "我是第一個元素"; //通過.innerText可以操作元素中的文字
li1.item(0).innerHTML = "<span style='color:red'>我是第二個元素</span>"; //通過.innerText可以操作元素中的文字且可以添加html代碼
//class
var lis = document.getElementsByClassName("item"); //返回HTMLCollection集合
cl(lis);
lis1 = Array.from(lis); //HTMLCollection類型無法使用forEach();可以通過Array.from()轉(zhuǎn)換成數(shù)組使用;
cl("轉(zhuǎn)換成數(shù)組", lis1);
cl(lis.length); //總數(shù)量
cl(lis[0]); //以數(shù)組的方式訪問HTMLCollection
cl(lis.item(1)); //以類數(shù)組對象中的item()的方法訪問HTMLCollection
//選中的元素和標簽可以通過的對象方法操作其屬性:.style操作樣式;
// CSS選擇器
var ul = document.querySelector("#list"); //返回html代碼
cl(ul);
var lis = document.querySelectorAll(".item"); //返回Nodelist集合
cl(lis);
lis.forEach(function (item) {
item.style.background = "lightgreen";
});
var first = document.querySelectorAll("#list>:nth-of-type(-n+2)");
var last = document.querySelectorAll("#list>:nth-of-type(n+4)");
cl(first);
cl(last);
cl(document.doctype); //獲取文檔類型
cl(document.documentElement); //獲取html文檔內(nèi)容不包含(文檔類型);
cl(window.document); //獲取html文檔所有內(nèi)容(包含文檔類型);
cl(document.title); //獲取html文檔標題title .body獲取主體內(nèi)容部分,.head獲取頭部信息內(nèi)容;
cl(document.links); //獲取文檔鏈接返回HTMLCollection集合
</script>
2.demo2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<ul id="fisrt">
<li>item01</li>
<li>item02</li>
<li>item03</li>
<li>item04</li>
<li>item05</li>
</ul>
</body>
<script>
var cl = console.log.bind(console);
/*js節(jié)點類型:
1.元素
2.屬性
3.文本
6.注釋
9.文檔
11.文檔片段
*/
var ul = document.querySelector("ul");
/* var li = document.createElement("li");
li.innerHTML = "item6";
//append和appendChild區(qū)別在于,append可以添加多個節(jié)點,而appendchild只能添加一個節(jié)點且不能直接添加字符串;
//且不會重復(fù)添加一個元素
ul.append(li);
ul.appendChild(li);*/
// nodeslist
cl(ul);
var lis = document.querySelectorAll("li");
cl(lis);
cl(ul.childNodes); //返回包含所有節(jié)點類型的集合
cl(ul.children); //只包含元素節(jié)點類型的集合
cl(ul.childElementCount); //統(tǒng)計元素節(jié)點的數(shù)量
cl(ul.childNodes.length); //統(tǒng)計所有類型節(jié)點的數(shù)量
cl(ul.childNodes[0]);
cl(ul.childNodes[1].nodeName); //返回元素標簽
cl(ul.childNodes[1].nodeType); //返回元素類型
cl(ul.childNodes[1].nodeValue); //返回元素值
cl(ul.firstChild); //返回第一個節(jié)點
cl(ul.firstElementChild); //返回第一個元素節(jié)點
cl(ul.lastChild); //返回最后一個節(jié)點
cl(ul.lastElementChild); //返回最后一個元素節(jié)點
cl("返回前一個節(jié)點(previousSibling)和下一個節(jié)點(nextSibling)");
cl(ul.childNodes[3]); //返回當前節(jié)點
cl(ul.childNodes[3].previousSibling); //返回當前節(jié)點的前一個節(jié)點
cl(ul.childNodes[3].nextSibling); //返回當前節(jié)點的前一個節(jié)點
cl("^…………");
cl(ul.childNodes[3]); //返回當前節(jié)點
cl(ul.childNodes[3].previousElementSibling); //返回當前節(jié)點的前一個元素節(jié)點
cl(ul.childNodes[3].nextElementSibling); //返回當前節(jié)點的前一個元素節(jié)點
</script>
</html>
3.demo3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>節(jié)點中className和classList</title>
<style>
.red {
color: red;
}
.item {
background-color: lightskyblue;
}
.blue {
color: limegreen;
}
</style>
</head>
<body>
<ul id="first">
<li class="item">item1</li>
<li class="item">item2</li>
<li class="item">item3</li>
<li class="item">item4</li>
<li class="item">item5</li>
<li class="item">item6</li>
</ul>
</body>
<script>
var cl = console.log.bind(console);
var ul = document.querySelector("ul");
cl(ul.id); //輸出元素中的id值
cl(ul.children[0].className); //輸出節(jié)點的class值,在JS中class是關(guān)鍵字所以使用className代替class
cl(ul.children[1].classList); //返回的是DOMTokenList對象
/*
classList對象中的方法:
1.add()增加值
2.remove()移除值
3.toggle()判斷,有此值則移除,無則添加
4.replace()替換
*/
ul.children[3].classList.add("red");
ul.children[4].classList.remove("item");
ul.children[3].classList.toggle("red"); //有則移除
ul.children[4].classList.toggle("red"); //無則添加
ul.children[2].classList.replace("item", "blue"); //把元素中的item替換成blue;
</script>
</html>
4.demo4
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>data-屬性</title>
</head>
<body>
<!-- html5中可以使用自定義數(shù)據(jù)屬性保存標簽的附加信息,data-做為前綴 -->
<ul data-index="1">
<li data-index="1">item1</li>
<li data-index="2">item2</li>
<li data-index-num="three">item3</li>
<li data-index="4">item4</li>
<li data-index="5">item5</li>
</ul>
</body>
<script>
var cl = console.log.bind(console);
var ul = document.querySelector("ul");
cl(ul);
cl(ul.children);
//data-index獲取其屬性時去掉data即dataset.index;
cl(ul.children[0].dataset.index);
//如果屬性名data-index-num時將中間線去掉,第二個單詞字母大寫,例如:dataset.indexNum;
cl(ul.children[2].dataset.indexNum); //獲取其屬性值
ul.children[2].dataset.indexNum = "3"; //修改其屬性值
</script>
</html>
5.demo5
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<!-- 事件觸犯和執(zhí)行都在元素上 -->
<span onclick="alert(this.innerText)">你好</span>
<br />
<!-- 事件觸犯在元素上,通過函數(shù)調(diào)用執(zhí)行JS中函數(shù)執(zhí)行 -->
<span onclick="show(this)">php中文網(wǎng)</span>
<p id="p">你好</p>
<span>種業(yè)圈</span>
</body>
<script>
function show(item) {
var text = item.innerText;
alert(text);
}
// 通過獲取元素綁定事件,并執(zhí)行
var p = document.querySelector("#p");
p.onclick = function () {
alert(this.nodeName);
};
//監(jiān)聽事件
var span = document.querySelector("body>span:last-of-type");
console.log(span);
span.addEventListener(
"click",
function (ev) {
alert(ev.target.innerText);
},
false
);
//創(chuàng)建委托事件來觸發(fā)
var ev = new Event("click"); //創(chuàng)建點擊事件對象
span.dispatchEvent(ev); //委派點擊事件來觸發(fā)span事件
</script>
</html>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號