天天看點

Html頁面Dom對象之ElementHTML DOM Element 對象

在 HTML DOM (文檔對象模型)中,每個部分都是節點:

文檔本身是文檔節點

所有 HTML 元素是元素節點

所有 HTML 屬性是屬性節點

HTML 元素内的文本是文本節點

注釋是注釋節點

在 HTML DOM 中,Element 對象表示 HTML 元素。

Element 對象可以擁有類型為元素節點、文本節點、注釋節點的子節點。

NodeList 對象表示節點清單,比如 HTML 元素的子節點集合。

元素也可以擁有屬性。屬性是屬性節點(參見下一節)。

IE

Firefox

Chrome

Safari

Opera

所有浏覽器都支援 Element 對象和 NodeList 對象。

下面的屬性和方法可用于所有 HTML 元素上:

屬性 / 方法

描述

<a href="http://www.w3school.com.cn/jsref/prop_html_accesskey.asp">element.accessKey</a>

設定或傳回元素的快捷鍵。

<a href="http://www.w3school.com.cn/jsref/met_node_appendchild.asp">element.appendChild()</a>

向元素添加新的子節點,作為最後一個子節點。

<a href="http://www.w3school.com.cn/jsref/prop_node_attributes.asp">element.attributes</a>

傳回元素屬性的 NamedNodeMap。

<a href="http://www.w3school.com.cn/jsref/prop_node_childnodes.asp">element.childNodes</a>

傳回元素子節點的 NodeList。

<a href="http://www.w3school.com.cn/jsref/prop_html_classname.asp">element.className</a>

設定或傳回元素的 class 屬性。

element.clientHeight

傳回元素的可見高度。

element.clientWidth

傳回元素的可見寬度。

<a href="http://www.w3school.com.cn/jsref/met_node_clonenode.asp">element.cloneNode()</a>

克隆元素。

<a href="http://www.w3school.com.cn/jsref/met_node_comparedocumentposition.asp">element.compareDocumentPosition()</a>

比較兩個元素的文檔位置。

<a href="http://www.w3school.com.cn/jsref/prop_html_contenteditable.asp">element.contentEditable</a>

設定或傳回元素的文本方向。

<a href="http://www.w3school.com.cn/jsref/prop_html_dir.asp">element.dir</a>

<a href="http://www.w3school.com.cn/jsref/prop_node_firstchild.asp">element.firstChild</a>

傳回元素的首個子。

<a href="http://www.w3school.com.cn/jsref/met_element_getattribute.asp">element.getAttribute()</a>

傳回元素節點的指定屬性值。

<a href="http://www.w3school.com.cn/jsref/met_element_getattributenode.asp">element.getAttributeNode()</a>

傳回指定的屬性節點。

<a href="http://www.w3school.com.cn/jsref/met_element_getelementsbytagname.asp">element.getElementsByTagName()</a>

傳回擁有指定标簽名的所有子元素的集合。

element.getFeature()

傳回實作了指定特性的 API 的某個對象。

element.getUserData()

傳回關聯元素上鍵的對象。

<a href="http://www.w3school.com.cn/jsref/met_element_hasattribute.asp">element.hasAttribute()</a>

如果元素擁有指定屬性,則傳回true,否則傳回 false。

<a href="http://www.w3school.com.cn/jsref/met_node_hasattributes.asp">element.hasAttributes()</a>

如果元素擁有屬性,則傳回 true,否則傳回 false。

<a href="http://www.w3school.com.cn/jsref/met_node_haschildnodes.asp">element.hasChildNodes()</a>

如果元素擁有子節點,則傳回 true,否則 false。

<a href="http://www.w3school.com.cn/jsref/prop_html_id.asp">element.id</a>

設定或傳回元素的 id。

<a href="http://www.w3school.com.cn/jsref/prop_html_innerhtml.asp">element.innerHTML</a>

設定或傳回元素的内容。

<a href="http://www.w3school.com.cn/jsref/met_node_insertbefore.asp">element.insertBefore()</a>

在指定的已有的子節點之前插入新節點。

<a href="http://www.w3school.com.cn/jsref/prop_html_iscontenteditable.asp">element.isContentEditable</a>

<a href="http://www.w3school.com.cn/jsref/met_node_isdefaultnamespace.asp">element.isDefaultNamespace()</a>

如果指定的 namespaceURI 是預設的,則傳回 true,否則傳回 false。

<a href="http://www.w3school.com.cn/jsref/met_node_isequalnode.asp">element.isEqualNode()</a>

檢查兩個元素是否相等。

<a href="http://www.w3school.com.cn/jsref/met_node_issamenode.asp">element.isSameNode()</a>

檢查兩個元素是否是相同的節點。

<a href="http://www.w3school.com.cn/jsref/met_node_issupported.asp">element.isSupported()</a>

如果元素支援指定特性,則傳回 true。

<a href="http://www.w3school.com.cn/jsref/prop_html_lang.asp">element.lang</a>

設定或傳回元素的語言代碼。

<a href="http://www.w3school.com.cn/jsref/prop_node_lastchild.asp">element.lastChild</a>

傳回元素的最後一個子元素。

<a href="http://www.w3school.com.cn/jsref/prop_node_namespaceuri.asp">element.namespaceURI</a>

傳回元素的 namespace URI。

<a href="http://www.w3school.com.cn/jsref/prop_node_nextsibling.asp">element.nextSibling</a>

傳回位于相同節點樹層級的下一個節點。

<a href="http://www.w3school.com.cn/jsref/prop_node_nodename.asp">element.nodeName</a>

傳回元素的名稱。

<a href="http://www.w3school.com.cn/jsref/prop_node_nodetype.asp">element.nodeType</a>

傳回元素的節點類型。

<a href="http://www.w3school.com.cn/jsref/prop_node_nodevalue.asp">element.nodeValue</a>

設定或傳回元素值。

<a href="http://www.w3school.com.cn/jsref/met_node_normalize.asp">element.normalize()</a>

合并元素中相鄰的文本節點,并移除空的文本節點。

element.offsetHeight

傳回元素的高度。

element.offsetWidth

傳回元素的寬度。

element.offsetLeft

傳回元素的水準偏移位置。

element.offsetParent

傳回元素的偏移容器。

element.offsetTop

傳回元素的垂直偏移位置。

<a href="http://www.w3school.com.cn/jsref/prop_node_ownerdocument.asp">element.ownerDocument</a>

傳回元素的根元素(文檔對象)。

<a href="http://www.w3school.com.cn/jsref/prop_node_parentnode.asp">element.parentNode</a>

傳回元素的父節點。

<a href="http://www.w3school.com.cn/jsref/prop_node_previoussibling.asp">element.previousSibling</a>

傳回位于相同節點樹層級的前一個元素。

<a href="http://www.w3school.com.cn/jsref/met_element_removeattribute.asp">element.removeAttribute()</a>

從元素中移除指定屬性。

<a href="http://www.w3school.com.cn/jsref/met_element_removeattributenode.asp">element.removeAttributeNode()</a>

移除指定的屬性節點,并傳回被移除的節點。

<a href="http://www.w3school.com.cn/jsref/met_node_removechild.asp">element.removeChild()</a>

從元素中移除子節點。

<a href="http://www.w3school.com.cn/jsref/met_node_replacechild.asp">element.replaceChild()</a>

替換元素中的子節點。

element.scrollHeight

傳回元素的整體高度。

element.scrollLeft

傳回元素左邊緣與視圖之間的距離。

element.scrollTop

傳回元素上邊緣與視圖之間的距離。

element.scrollWidth

傳回元素的整體寬度。

<a href="http://www.w3school.com.cn/jsref/met_element_setattribute.asp">element.setAttribute()</a>

把指定屬性設定或更改為指定值。

<a href="http://www.w3school.com.cn/jsref/met_element_setattributenode.asp">element.setAttributeNode()</a>

設定或更改指定屬性節點。

element.setIdAttribute()

element.setIdAttributeNode()

element.setUserData()

把對象關聯到元素上的鍵。

element.style

設定或傳回元素的 style 屬性。

<a href="http://www.w3school.com.cn/jsref/prop_html_tabindex.asp">element.tabIndex</a>

設定或傳回元素的 tab 鍵控制次序。

<a href="http://www.w3school.com.cn/jsref/prop_element_tagname.asp">element.tagName</a>

傳回元素的标簽名。

<a href="http://www.w3school.com.cn/jsref/prop_node_textcontent.asp">element.textContent</a>

設定或傳回節點及其後代的文本内容。

<a href="http://www.w3school.com.cn/jsref/prop_html_title.asp">element.title</a>

設定或傳回元素的 title 屬性。

element.toString()

把元素轉換為字元串。

<a href="http://www.w3school.com.cn/jsref/met_nodelist_item.asp">nodelist.item()</a>

傳回 NodeList 中位于指定下标的節點。

<a href="http://www.w3school.com.cn/jsref/prop_nodelist_length.asp">nodelist.length</a>

傳回 NodeList 中的節點數。

繼續閱讀