所有的HTML元素都由HTMLElement類型表示,HTMLElement類型繼承自Element並添加了一些屬性,包括:
- id
- title
- dir
- lang
className
操作特性
操作特性的三個方法
- getAttribute();
- setAttribute();
removeAttribute();
這三個方法可操作所有特性,包括之前提到的5個屬性以及自定義特性;
但是自定義特性不能通過屬性方法訪問,即不能通過.來訪問
<div id="my_id" my_special="special"></div>
alert(div.id); //"my_id"
alert(div.my_special); //undefined(IE除外)
例外
- style:通過getAttribute()訪問得到css文本,通過屬性方法訪問得到對象
onClick:通過getAttribute()訪問的到函數的字符串,通過屬性方法訪問的到函數
setAttribute()方法對於不存在的特性會自動創建
div.my_color="red";
alert(div.getAttribute("my_color")); //null(IE除外)
attributes
這個屬性包括了所有特性的集合
childNodes
IE會包括所有子節點以及由空白符組成的文本節點,其他瀏覽器不會