*自定義類數(shù)組
// 數(shù)組
student=['張三','李四','悟空','八戒','小白龍','悟凈'];
// 類數(shù)組
student2={
0:'張三',
1:'李四',
2:'悟空',
3:'八戒',
4:'小白龍',
5:'悟凈',
length: 6
};
純數(shù)組/真數(shù)組的特征:
1.每個值對應(yīng)的鍵名: 是從 0 開始遞增正整數(shù), 如 0,1,2,3,…
類數(shù)組 與 純數(shù)組的區(qū)別:
數(shù)組的構(gòu)造器是 Prototype:Arry
類數(shù)組的構(gòu)造器是 Prototype:Object
Arry是Object的一個子類。
應(yīng)用場景:類數(shù)組‘等同于’純數(shù)組,可用于前后端的數(shù)據(jù)讀取,交互,當然也需要進行轉(zhuǎn)換。
API 2個
1.獲取一組數(shù)據(jù)(類數(shù)組):querySelectorAll()
2.獲取一個數(shù),返回是Dom對象:querySelector()
使用場景:獲取一組或者單個對象時使用,按需選擇。
<form action="" method="post" id="login">
<label for="uname"> 賬戶名:</label>
<input type="uname" name="uname" id="uname" value="phpcn">
<br>
<label for="pwd">
密碼:<input type="password" name="password" id="pwd">
</label>
<br>
<button>登錄</button>
</form>
例如:登錄表單 表單id=’login’
更優(yōu)雅 - 獲取表單:document.forms.login //login合法標識符 或者document.forms[‘login’]
獲取input控件的value值:
//先獲取login表單,再獲取表單里面的元素,再得到值
console.log(document.forms.login.uname.value)
常用API:
a = DOM節(jié)點數(shù)組
1.類數(shù)組轉(zhuǎn)為真數(shù)組:Array.from()/[...類數(shù)組.children]
2.循環(huán)forEach()
3.獲取第一個節(jié)點:a.firstElementChild
4.獲取下一個:a.firstElementChild.nextElementSibling
5.獲取最后一個:a.lastElementChild
6.獲取前一個:a.lastElementChild.previousElementSibling
7.獲取父節(jié)點:a.lastElementChild.parentElement
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號