{"id":12213,"date":"2021-01-15T00:00:00","date_gmt":"2021-01-14T16:00:00","guid":{"rendered":"https:\/\/fgchen.com\/wpedu2\/2021\/01\/15\/javascript%e5%bf%ab%e9%80%9f%e5%85%a5%e9%96%80-dom-html-document-object-model\/"},"modified":"2026-03-30T14:39:23","modified_gmt":"2026-03-30T06:39:23","slug":"javascript%e5%bf%ab%e9%80%9f%e5%85%a5%e9%96%80-dom-html-document-object-model","status":"publish","type":"post","link":"https:\/\/fgchen.com\/wpedu\/2021\/01\/javascript%e5%bf%ab%e9%80%9f%e5%85%a5%e9%96%80-dom-html-document-object-model\/","title":{"rendered":"JavaScript\u5feb\u901f\u5165\u9580 &#8211;  DOM (HTML Document Object Model)"},"content":{"rendered":"<h1 id=\"mcetoc_1eqibsmcp10\"><b>DOM \u6587\u4ef6\u7269\u4ef6\u6a21\u578b<\/b><\/h1>\n\nDOM\u662fJavaScript\u5c07\u6574\u500bHTML\u6587\u4ef6\u8f49\u63db\u6210\u4e00\u500b\u4ee5document\u7269\u4ef6\u70ba\u6839\u7bc0\u9ede\u7684\u7269\u4ef6\u968e\u5c64\u6a21\u578b\u3002JavaScript\u900f\u904eDOM\u4f86\u64cd\u4f5cHTML\u6587\u4ef6\u5143\u7d20\u7684\u5167\u5bb9(\u5305\u542b\u683c\u5f0f)\uff0c\u4e5f\u5c31\u662f\u8aaa\uff0cJavaScript\u52d5\u614b\u5730\u900f\u904eDOM\u4f86\u8b8a\u66f4\u8207\u53d6\u5f97HTML\u5143\u7d20\u7684\u503c\u8207\u683c\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u52d5\u614b\u5730\u5728DOM\u6a39\u4e2d\u522a\u9664\u8207\u52a0\u5165\u4e00\u500b\u7bc0\u9ede\u7269\u4ef6\uff0c\u9032\u800c\u8b8a\u66f4HTML\u6587\u4ef6\u7684\u5167\u5bb9\u3002\n\n<img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/614\/1*WvPNmqXTSTijB_mSO8PMjA.png\" width=\"614\" height=\"420\" class=\"alignnone size-full\" \/>\n\n&nbsp;\n\n<h2 id=\"mcetoc_1eqibsp8a11\"><b>\u7bc0\u9ede\u5c6c\u6027\/Node Properties\u00a0<\/b><\/h2>\n\n<b>attributes\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns a live collection of all attributes registered to and element\u00a0<\/span>\n\n<b>baseURI\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Provides the absolute base URL of an HTML element\u00a0<\/span>\n\n<b>childNodes\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Gives a collection of an element\u2019s child nodes\u00a0<\/span>\n\n<b>firstChild\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns the first child node of an element\u00a0<\/span>\n\n<b>lastChild\u00a0<\/b>\n\n<span style=\"font-weight: 400\">The last child node of an element\u00a0<\/span>\n\n<b>nextSibling\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Gives you the next node at the same node tree level\u00a0<\/span>\n\n<b>nodeName\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns the name of a node\u00a0<\/span>\n\n<b>nodeType\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns the type of a node\u00a0<\/span>\n\n<b>nodeValue\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Sets or returns the value of a node\u00a0<\/span>\n\n<b>ownerDocument\u00a0<\/b>\n\n<span style=\"font-weight: 400\">The top-level document object for this node\u00a0<\/span>\n\n<b>parentNode\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns the parent node of an element\u00a0<\/span>\n\n<b>previousSibling\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns the node immediately preceding the current one\u00a0<\/span>\n\n<b>textContent\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Sets or returns the textual content of a node and its descendants\u00a0<\/span>\n\n<h2 id=\"mcetoc_1eqibt1d212\"><b>\u7bc0\u9ede\u65b9\u6cd5\/Node Methods\u00a0<\/b><\/h2>\n\n<b>appendChild()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Adds a new child node to an element as the last child node\u00a0<\/span>\n\n<b>cloneNode()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Clones an HTML element\u00a0<\/span>\n\n<b>compareDocumentPosition()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Compares the document position of two elements\u00a0<\/span>\n\n<b>getFeature()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns an object which implements the APIs of a specified feature\u00a0<\/span>\n\n<b>hasAttributes()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns true if an element has any attributes, otherwise false\u00a0<\/span>\n\n<b>hasChildNodes()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns true if an element has any child nodes, otherwise false\u00a0<\/span>\n\n<b>insertBefore()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Inserts a new child node before a specified, existing child node\u00a0<\/span>\n\n<b>isDefaultNamespace()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns true if a specified namespaceURI is the default, otherwise false\u00a0<\/span>\n\n<b>isEqualNode()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Checks if two elements are equal\u00a0<\/span>\n\n<b>isSameNode()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Checks if two elements are the same node\u00a0<\/span>\n\n<b>isSupported()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns true if a specified feature is supported on the element\u00a0<\/span>\n\n<b>lookupNamespaceURI()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns the namespaceURI associated with a given node\u00a0<\/span>\n\n<b>lookupPrefix()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns a DOMString containing the prefix for a given namespaceURI, if present\u00a0<\/span>\n\n<b>normalize()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Joins adjacent text nodes and removes empty text nodes in an element\u00a0<\/span>\n\n<b>removeChild()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Removes a child node from an element\u00a0<\/span>\n\n<b>replaceChild()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Replaces a child node in an element\u00a0<\/span>\n\n<h2 id=\"mcetoc_1eqibtmel13\"><b>\u5143\u7d20\u65b9\u6cd5\/Element Methods\u00a0<\/b><\/h2>\n\n<b>getAttribute()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">\u56de\u50b3\u4e00\u500b\u5143\u7d20\u7bc0\u9ede\u7684\u6307\u5b9a\u5c6c\u6027\u7684\u503c\u3002\u4f8b\uff1a<\/span>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/\u53d6\u5f97HTML\u6587\u4ef6\u4e2d\u7b2c1\u500bH1\u6a19\u7c64\u5143\u7d20\u7684class\u5c6c\u6027\u503c\uff0c\u82e5HTML\u6587\u4ef6\u4e2d\u7b2c1\u500bH1\u6a19\u7c64\u5b9a\u7fa9\u5982\u4e0b\uff1a\n\/\/&lt;H1 class=\"color font\"&gt;\n\/\/\u4e0b\u9762\u6558\u8ff0x\u503c\u70ba\"color font\"\nvar x = document.getElementsByTagName(\"H1\")[0].getAttribute(\"class\");\nconsole.log(x);\n\/\/\u8f38\u51fa \"color font\"\n<\/pre>\n\n&nbsp;\n\n<b>getAttributeNode()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Gets the specified attribute node\u00a0<\/span>\n\n<b>getElementsByTagName()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Provides a collection of all child elements with the specified tag name\u00a0<\/span>\n\n<b>hasAttribute()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Returns true if an element has any attributes, otherwise false\u00a0<\/span>\n\n<b>removeAttribute()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Removes a specified attribute from an element\u00a0<\/span>\n\n<b>removeAttributeNode()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Takes away a specified attribute node and returns the removed node\u00a0<\/span>\n\n<b>setAttribute()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Sets or changes the specified attribute to a specified value\u00a0<\/span>\n\n<b>setAttributeNode()\u00a0<\/b>\n\n<span style=\"font-weight: 400\">Sets or changes the specified attribute node\u00a0<\/span>\n\n&nbsp;","protected":false},"excerpt":{"rendered":"<p>DOM \u6587\u4ef6\u7269\u4ef6\u6a21\u578b DOM\u662fJavaScript\u5c07\u6574\u500bHTML\u6587\u4ef6\u8f49\u63db\u6210\u4e00\u500b\u4ee5 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[266],"tags":[],"class_list":["post-12213","post","type-post","status-publish","format-standard","hentry","category-266"],"_links":{"self":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/12213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/comments?post=12213"}],"version-history":[{"count":1,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/12213\/revisions"}],"predecessor-version":[{"id":13260,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/posts\/12213\/revisions\/13260"}],"wp:attachment":[{"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/media?parent=12213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/categories?post=12213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgchen.com\/wpedu\/wp-json\/wp\/v2\/tags?post=12213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}