dom相關(guān)
- document.getElementById(“box”);//通過id獲取標(biāo)簽
- document.getElementsByclassName(“box1”) 根據(jù)類名獲取頁面元素
- document.getElementsByTagName(“div”);根據(jù)標(biāo)簽名獲取頁面元素
操作節(jié)點 (一定是父節(jié)點去調(diào)用方法,操作的都是子節(jié)點)
- 從后面添加子節(jié)點:
節(jié)點.appendChild(傳入的節(jié)點);
- 從前面添加子節(jié)點:
節(jié)點.insertBefore(要插入的節(jié)點 , 在誰之前);
- 刪除子節(jié)點
父節(jié)點.removeChild(子節(jié)點)
屬性相關(guān)
- setAttribute 給元素設(shè)置屬性:
element.setAttribute(name, value);
- getAttribute 獲取元素的某個屬性:
var value = element.getAttribute("id");
- hasAttribute 元素是否又某個屬性:
var bool = element.getAttribute("pid");
- 設(shè)置標(biāo)簽的內(nèi)容
box.innerHtml=”內(nèi)容加進(jìn)去”需忿;
- 訪問類名
標(biāo)簽.className
樣式相關(guān)
- 對標(biāo)簽的樣式設(shè)置使用
.style
var box=document.getElementById("box");
box.style.width="100px";
box.style.backgroundColor="#ff00000"http://帶有段橫線的屬性要是用駝峰命名的方式诅炉。
- 同時設(shè)置標(biāo)簽的多個樣式
box.style.cssText
="width:10px;height:100px;background-color:red";//不常用
表單的屬性
- 禁用s輸入框
ipt.disabled = boolean
- 復(fù)選框選中
cbx.checked = boolean
- 下拉框選中
slt.selected = boolean
事件類型
- 點擊事件
onclick
- 鼠標(biāo)移入
onmouseover
- 鼠標(biāo)移出
onmouseout
- 標(biāo)簽?zāi)J(rèn)事件取消
某些標(biāo)簽具有默認(rèn)的效果,例如a標(biāo)簽的跳轉(zhuǎn)屋厘,有時候我們不想要這種效果涕烧,可以在onclick事件最后加上return false;
焦點事件
- focus() 讓元素獲得焦點
ipt.focus()
- onfocus 文本框獲取焦點時觸發(fā)事件
ipt.onfocus=function(){...}
- onblur 失去焦點時觸發(fā)事件
- input獲取內(nèi)部文本 使用value
ipt.value