
批改狀態(tài):合格
老師批語:
// 通過form表單的id獲取整個表單
// document.forms可獲取頁面所有表單,加上索引[0]或.id可獲取指定的表單
console.log(document.forms[0]);
console.log(document.forms.frm);
// 獲取表單內(nèi)的元素,可通過元素的name值來獲?。篺orms.表單id.元素name
console.log(document.forms.frm.username);
// 獲取元素的值:forms.表單id.元素name.value,獲取其他屬性值也一樣,如:username.name;username.type
console.log(document.forms.frm.username.value);
// dom樹的遍歷與常用屬性
// 1.獲取整個list元素
let div=document.querySelector('.list');
console.log(div);
// 2.獲取所有子節(jié)點元素:children,獲取到的結(jié)果是一個元素集合。
let child=div.children;
console.log(child);
// 3.將元素集合轉(zhuǎn)成數(shù)組 Array.from(集合) 或者 ...集合
const arr=Array.from(child);
console.log(arr);
const arr2=[...child];
console.log(arr2);
// 常用屬性
// 1.元素集合.firstElementChild可獲取集合中的第一個元素。
console.log(child);
div.firstElementChild.style.backgroundColor='#f60';
// 2.獲取最后一個
div.lastElementChild.style.backgroundColor='yellow';
// 獲取下一個兄弟元素
div.firstElementChild.nextElementSibling.style.backgroundColor='green';
// 獲取上一個兄弟元素
div.lastElementChild.previousElementSibling.style.backgroundColor='red';
// 獲取父節(jié)點 parentElement或parentNode
div.lastElementChild.parentElement.style.border='5px solid #ccc';
// 創(chuàng)建元素 document.createElement
ul=document.createElement('ul');
document.body.append (ul);
// 創(chuàng)建子元素li
for(let i=0;i<5;i++){
let li=document.createElement('li');
li.textContent='item'+(i+1);
ul.append(li);
}
// 查看元素
// 1.outerHtml獲取元素的html字符串
console.log(ul.outerHTML);
// 在節(jié)點之前插入元素before()
let newLi=document.createElement('li');
newLi.textContent='New-Item';
newLi.style.color='#f60';
// 例:在第4個元素前面插入新建的元素
// 1.先獲取第4個元素
let four=document.querySelector('li:nth-of-type(4)');
// 2.插入
four.before(newLi);
// 在節(jié)點之后插入after()
// 插入節(jié)點時必需新建或克隆節(jié)點,不能用之前創(chuàng)建的,否則就只是把之前創(chuàng)建的元素剪切移動到此處,之前插入的位置變?yōu)榭樟恕?/span>
// 例:在第4個節(jié)點之后插入新節(jié)點
// 1.先創(chuàng)建或克隆新元素:克隆方法:元素.cloneNode(true),參數(shù)true如果不寫只克隆節(jié)點不克隆文本。
let fourAfter=newLi.cloneNode(true);
// 2.插入
four.after(fourAfter);
// 在父節(jié)點插入元素insertAdjacentElement('插入位置',元素):有4個位置,節(jié)點開始位置之前beforeBegin、開始位置之后afterBegin、節(jié)點結(jié)束位置之前beforeEnd、結(jié)束位置之后afterEnd
// 例:在UL前面插入一個h3
// 1.先創(chuàng)建h3元素
let H3=document.createElement('h3');
H3.textContent='好好學(xué)習(xí),天天向上!';
console.log(H3);
ul.insertAdjacentElement('beforeBegin',H3);
// 替換元素replaceChild
// 1.創(chuàng)建一個新元素
let a=document.createElement('a');
a.href='https://baidu.com';
a.textContent='百度';
// 2.執(zhí)行替換最后一個元素
let last=ul.lastElementChild;
ul.replaceChild(a,last);
// 刪除元素remove(節(jié)點)
ul.lastElementChild.remove(last);
// textContent:返回當(dāng)前元素及所有后代元素 標(biāo)簽內(nèi)的文本內(nèi)容(不含html標(biāo)簽本身),包括隱藏內(nèi)容(display:none
let box=document.querySelector('.box');
console.log(box.textContent);
// innerText:僅返回可見的文本
console.log(box.innerText);
// innerHTML:返回所有內(nèi)容,包括html標(biāo)簽、style、文本(包括隱藏的)等全部內(nèi)容
console.log(box.innerHTML);
// outerHTML:返回元素本身所有的html字符串
console.log(box.outerHTML);
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{padding: 0;margin: 0;box-sizing: border-box;}
.lycontent {
width: 25em;
height: 3em;
border: 1px solid rgb(3, 165, 138);
padding: 0 10px;
border-radius: 5px;
}
ul{ list-style: none;background-color: bisque;width: 20.8em;margin: -1em 0 0 8.4em;}
li{ height: 2em;line-height: 2em;border-bottom: 1px dashed #ccc;padding-left:0.5em;}
input{margin: 2em 10em;}
</style>
</head>
<body>
<input type="text" placeholder="請輸入留言內(nèi)容" class="lycontent" onkeydown="addmsg(this);">
<ul class="lylist"></ul>
<script>
function addmsg(obj){
// 判斷按鍵,如果是回車鍵則執(zhí)行添加留言
if(event.keyCode==13){
// 獲取文本框元素
let input=document.querySelector('.lycontent');
// 獲取文本框輸入內(nèi)容
let con=input.value;
// 判斷輸入內(nèi)容是否為空
if(con==''){
alert('總得說點啥吧!');
return false;
}
// 獲取ul元素,作為父級插入使用
let ul=document.querySelector('ul');
// 新建li元素,并賦值輸入的內(nèi)容
let li=document.createElement('li');
li.textContent=con;
// 根據(jù)留言規(guī)則,最新的一條顯示在最上面,故應(yīng)將li元素插入父節(jié)點開始位置的下方
ul.insertAdjacentElement('afterBegin',li);
// 清空文本框并獲取焦點
input.value='';
input.focus();
}
}
</script>
</body>
<ul>
<!-- data-開頭的為自定義屬性,用dataset.加上data-后面的自定義內(nèi)容即可 -->
<li class="item" data-types="電腦">item1</li>
<li class="item" data-types="手機(jī)">item2</li>
<li class="item" data-types="平板">item3</li>
<li class="item" data-types="MP3">item4</li>
<li class="item" data-types="電視">item5</li>
</ul>
<div data-me-name='松濤'>Hello World!</div>
<script>
let li=document.querySelectorAll('li');
for(let i=0;i<li.length;i++){
// 獲取方式1:去掉data-,直接通過元素.dataset.屬性。例如:li設(shè)置的自定義屬性為data-type='pc',則獲取時通過li.dataset.type
li[i].onclick=()=>console.log(li[i].dataset.types);
}
// 獲取方式2:如果定義成這種形式:data-me-name,也是先去掉data-,然后將me-name轉(zhuǎn)為小駝峰寫法:meName
let div=document.querySelector('div');
console.log(div.dataset.meName);
// dataset是可讀寫屬性,即可改變元素的自定義屬性值
div.dataset.meName='小明';
</script>
<style>
.box{ width: 10em;height: 10em;}
</style>
<body>
<div class="box" style="color: rgb(22, 22, 21);background-color: lightblue;">Hello World!</div>
</body>
// 獲取元素樣式分兩種情況:1.元素行內(nèi)樣式,即定義在元素標(biāo)簽內(nèi)的。如:<div style='color:red'>Hello</div>
// 2.文檔內(nèi)定義在<style></style>標(biāo)簽中的樣式以及外部引入的CSS樣式,這些都屬性計算樣式。
// 獲取第1種情況的方法:利用元素的style屬性獲取 **行內(nèi)樣式** 的值
// 此方式只能獲取元素行內(nèi)樣式定義的屬性值,不能獲取<style></style>標(biāo)簽中定義的以及外部引入的CSS樣式。
let div=document.querySelector('div');
console.log(div.style.color);
console.log(div.style.backgroundColor);
// 獲取第2種情況的方法:需要通過window對象的計算樣式屬性來獲取:window.getComputedStyle(元素).屬性
// 此方式獲取到的寬高度等有數(shù)值的屬性是帶單位的,如高度值表現(xiàn)為200px,如果只想要數(shù)值不想要單位,可以通過parseInt轉(zhuǎn)換一下。
let width=window.getComputedStyle(div).width;
console.log(width); // 打印 160px
console.log(parseInt(width)); // 打印 160
<style>
.blue { color: blue;}
.red { color: red;}
.green { color: green;}
</style>
<body>
<!-- 操作元素的class屬性 -->
<p>PHP中文網(wǎng)</p>
<script>
// 通過元素的classList對象來操作class
let p=document.querySelector("p");
console.log(typeof p.classList); // 打印object
// 1.add添加樣式,可以疊加
p.classList.add('blue');
p.classList.add('red');
// 2.contains 判斷某個樣式是否存在,返回值:true/false
console.log(p.classList.contains('blue')); // 返回true
// 3.replace(舊樣式,新樣式)替換樣式
p.classList.replace('red','green'); // 返回值為true/false 即替換成功與否,重復(fù)替換返回false
// 4.remove(樣式)刪除樣式
p.classList.remove('green'); //無返回值(undefined)
// 5.toggle切換樣式
p.classList.toggle('red');
</script>
</body>
<body>
<button>按鈕一</button>
<button>按鈕二</button>
<button>按鈕三</button>
<script>
// 事件的添加方式一:先獲取元素,然后通過元素.onclick(){}進(jìn)行添加
let btn1=document.querySelector('button');
btn1.onclick=()=>console.log(btn1.textContent);
// 刪除事件:null
btn1.onclick=null;
// 方式二:事件監(jiān)聽器 addEventListener('事件click等',函數(shù)名)
// 第二個參數(shù):函數(shù),可以直接寫回調(diào),但不能移除;想要移除,必需得是命名函數(shù)且在外部聲明
// 注:函數(shù)名不用加(),也不用加'',否則報錯。
let btn2=document.querySelector('button:nth-of-type(2)');
btn2.addEventListener('click',getName);
function getName(){
console.log(1234);
}
// 移除事件
btn2.removeEventListener('click',getName); // 參數(shù)與添加一致
// 事件派發(fā):讓事件自動執(zhí)行點擊等事件操作,無需干預(yù)。
// 以按鈕自動執(zhí)行點擊操作為例
let btn3=document.querySelector('button:nth-of-type(3)');
// 1.給按鈕添加點擊事件
let money=0;
btn3.addEventListener('click',()=>{
console.log(money);
money+=1.2;
})
// 2.創(chuàng)建一個自定義事件:用new Event構(gòu)造函數(shù)生成自定義事件,對標(biāo)按鈕的點擊事件click
// console.log(typeof new Event('click')); // 返回一個對象類型 object
let getMoney=new Event('click');
// 3.實施事件派發(fā) dispatchEvent(自定義事件名)
btn3.dispatchEvent(getMoney);
// 4.通過setTimeout可以延時執(zhí)行;通過setInterval可以定時執(zhí)行
setTimeout(()=>btn3.dispatchEvent(getMoney),1000); //延時派發(fā)
setInterval(()=>btn3.dispatchEvent(getMoney),1000); //定時派發(fā)
</script>
</body>
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號