1. 程式人生 > 資訊 >華碩新款天選 Air 輕薄遊戲本開啟預約:i7-12650H + RTX 30 顯示卡,3 月 8 日開賣

華碩新款天選 Air 輕薄遊戲本開啟預約:i7-12650H + RTX 30 顯示卡,3 月 8 日開賣

元素獲得

原始方式
1. var x = document.documentElement;
2. var x = document.body;
3. var x = document.getElementById(“aBC”)
4. var x = document.getElementByTagName(“p”)

選擇器
1. document.querySelector("div p.red")
2. document.querySelectorAll("div p.red")

遍歷DOM樹
.parentNode  . children   .firstChild   .lastChild 
.nextElementSibling  .priviousElementSibing

元素建立與刪除

1. document.createElement(新)
2. .appendChild(新) .insertBefore(新,item) .replaceChild(新,舊) .removeChild(舊)  元素.cloneNode()
3. document.createTextNode()
4. document.getElementById('myAnchor').innerHTML="RUNOOB";
5. 父.removeChild(子)

屬性獲得

元素.attributes
使用 for(var i=0;i<elem.attributes.length;i++){
 xxx = elem.attributes[i];
}

屬性建立與修改

1. .getAttribute(“屬性”)
2. .removeAttribute(“屬性名”) //刪除屬性及屬性值。
3. .setAttribute(“屬性名”,”屬性值”) 屬性不存在,建立;若存在,替換屬性值。
4. .style.屬性=新樣式
    document.getElementById("p2").style.color="blue";

新增事件

1. document.getElementById("myBtn").onclick=function(){myFunction ()};
2. document.getElementById("myBtn").addEventListener("click", myFunction);
注:addEventListener(event, function, useCapture);
useCapture :false冒泡(預設), true捕獲
3. element.removeEventListener("mousemove", myFunction);