創(chuàng)建dom
//創(chuàng)建一個li標(biāo)簽
var oLi = document.createElement("li");
//父.appendChild(oLi);插到后面的位置(變成最后一個子節(jié)點)
//兄弟.insertBefore(oLi);插到這個兄弟節(jié)點之前
刪除一個dom
//父.removeChild(oLi)
.parentNode()
文檔碎片增快速度
var fragment = document.createDocumentFragment();
var oLi = document.createElement("li");
oLi.innerHTML = "Xujiguang";
//oLi.innerText = "xu";
fragment.appendChild(oLi); //然后把fragment加到父節(jié)點
dom節(jié)點
oUl.childNodes //獲取子節(jié)點,只是子一層疲憋,在firefox中空行也會算子節(jié)點(文本節(jié)點)
//獲取節(jié)點種類
document.body.childNodes[0].NodeType
// 3 -- 文本節(jié)點
//1 -- 元素節(jié)點
//火狐中獲取子節(jié)點要和NodeType結(jié)合才能正確的獲取子節(jié)點
//
oUl.children //獲取子節(jié)點(一層)斋日,不算文本節(jié)點childNodes的兼容版本
//
oLi.parentNode //獲取父節(jié)點
//
//隱藏節(jié)點
this.parentNode.style.display = none;
offsetParent
ps:小知識
position:absolute 從文檔流中脫出介劫,根據(jù)left,right,top,buttom進(jìn)行定位
(相對于 static 定位以外的第一個父元素進(jìn)行定位)
//
position:relative 對象不可層疊,但將依據(jù)left挚歧,right映挂,top,bottom等屬性
在正常文檔流中偏移位置(相對于自身在原來的文檔呢流中的位置)
offsetParent獲取相對定位的父對象
繼續(xù)子節(jié)點
var oCh = oUl.firstElementChild || oUl.firstChild
var oCh = oUl.lastElementChild || oUl.lastChild
兄弟節(jié)點
oLi.previousSibling || oLi.previousElementSibling
//
oLi.nextSibling || oLi.nextElemetnSibling
操縱元素屬性
//
oIn.value = "123"; //設(shè)置表單控件的value
//
oIn["value"] = "123";
//
oIn.setAttribute("value", "xujiguagn");
//獲取元素屬性
var v = oIn.getAttribute("id");
//刪掉元素屬性
oIn.removeAttribute("class");
//
oIn.style.display = "none";
oIn.style["display"] = "none";
通過class選取元素
function getByClass(oParent, sClass) {
var aEle = oParent.getElementsByTagName("*");
var aReault = [];
for (var i = 0; i< aEle.length; i++) {
if(aEle[i].className == sClass) {
aResult.push(aEle[i]);
}
}
}
js事件
//window的事件包括onload坊秸、onscroll麸祷、onresize
//其他的事件
window.open("www.baidu.com", "_self"); //可以指定在哪個frame打開澎怒,默認(rèn)_blank
最后編輯于 :2017.12.03 03:50:32
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者