{"id":237699,"date":"2022-03-27T10:01:10","date_gmt":"2022-03-27T02:01:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=237699"},"modified":"2022-03-14T10:03:46","modified_gmt":"2022-03-14T02:03:46","slug":"dom-node-list","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/dom-node-list.html","title":{"rendered":"DOM \u8282\u70b9\u5217\u8868\uff08Node List\uff09\u6982\u8ff0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8282\u70b9\u5217\u8868\u7531 getElementsByTagName() \u65b9\u6cd5\u548c childNodes \u5c5e\u6027\u8fd4\u56de\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
DOM \u8282\u70b9\u5217\u8868\uff08Node List\uff09<\/strong><\/div>\n

\u5f53\u4f7f\u7528\u8bf8\u5982 childNodes \u6216 getElementsByTagName() \u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u65f6\uff0c\u4f1a\u8fd4\u56de\u8282\u70b9\u5217\u8868\u5bf9\u8c61\u3002<\/p>\n

\u8282\u70b9\u5217\u8868\u5bf9\u8c61\u8868\u793a\u8282\u70b9\u7684\u5217\u8868\uff0c\u4e0e XML \u4e2d\u7684\u987a\u5e8f\u76f8\u540c\u3002<\/p>\n

\u8282\u70b9\u5217\u8868\u4e2d\u7684\u8282\u70b9\u4f7f\u7528\u4ece 0 \u5f00\u59cb\u7684\u7d22\u5f15\u53f7\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n

\u4e0b\u9762\u7684\u56fe\u50cf\u8868\u793a \"books.xml\" \u4e2d <title> \u5143\u7d20\u7684\u8282\u70b9\u5217\u8868\uff1a
\n\"\"<\/p>\n

\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u901a\u8fc7\u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d\uff0c\u5e76\u8fd4\u56de \"books.xml\" \u4e2d title \u5143\u7d20\u7684\u8282\u70b9\u5217\u8868\uff1a<\/p>\n

xmlDoc=loadXMLDoc(\"books.xml\");\r\n\r\nx=xmlDoc.getElementsByTagName(\"title\");<\/pre>\n

\u5728\u4e0a\u9762\u7684\u8bed\u53e5\u6267\u884c\u4e4b\u540e\uff0cx \u662f\u8282\u70b9\u5217\u8868\u5bf9\u8c61\u3002<\/p>\n

\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4ece\u8282\u70b9\u5217\u8868\uff08x\uff09\u4e2d\u7684\u7b2c\u4e00\u4e2a<title> \u5143\u7d20\u8fd4\u56de\u6587\u672c\uff1a<\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

txt=x[0].childNodes[0].nodeValue;<\/pre>\n

\u5728\u4e0a\u9762\u7684\u8bed\u53e5\u6267\u884c\u4e4b\u540e\uff0ctxt = \"Everyday Italian\"\u3002<\/p>\n

\u8282\u70b9\u5217\u8868\u957f\u5ea6\uff08Node List Length\uff09<\/strong><\/div>\n

\u8282\u70b9\u5217\u8868\u5bf9\u8c61\u4f1a\u4fdd\u6301\u81ea\u8eab\u7684\u66f4\u65b0\u3002\u5982\u679c\u5220\u9664\u6216\u6dfb\u52a0\u4e86\u5143\u7d20\uff0c\u5217\u8868\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n

\u8282\u70b9\u5217\u8868\u7684 length \u5c5e\u6027\u662f\u5217\u8868\u4e2d\u8282\u70b9\u7684\u6570\u91cf\u3002<\/p>\n

\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u901a\u8fc7\u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d\uff0c\u5e76\u8fd4\u56de \"books.xml\" \u4e2d <title> \u5143\u7d20\u7684\u6570\u91cf\uff1a<\/p>\n

xmlDoc=loadXMLDoc(\"books.xml\");\r\n\r\nx=xmlDoc.getElementsByTagName('title').length;<\/pre>\n

\u5728\u4e0a\u9762\u7684\u8bed\u53e5\u6267\u884c\u4e4b\u540e\uff0cx = 4\u3002<\/p>\n

\u8282\u70b9\u5217\u8868\u7684\u957f\u5ea6\u53ef\u7528\u4e8e\u904d\u5386\u5217\u8868\u4e2d\u6240\u6709\u7684\u5143\u7d20\u3002<\/p>\n

\u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u4f7f\u7528 length \u5c5e\u6027\u6765\u904d\u5386 <title> \u5143\u7d20\u7684\u5217\u8868\uff1a<\/p>\n

\u5b9e\u4f8b<\/strong><\/p>\n

xmlDoc<\/span>=<\/span>loadXMLDoc<\/span>(<\/span>\"<\/span>books.xml<\/span>\"<\/span>)<\/span>; \r\n<\/span>\/\/<\/span> x \u53d8\u91cf\u83b7\u53d6\u6307\u5b9a\u8282\u70b9\u5217\u8868<\/span><\/span>\r\n<\/span>x<\/span>=<\/span>xmlDoc<\/span>.<\/span>getElementsByTagName<\/span>(<\/span>'<\/span>title<\/span>'<\/span>)<\/span>; \r\n<\/span>for<\/span> <\/span>(<\/span>i<\/span>=<\/span>0<\/span>;<\/span>i<\/span><<\/span>x<\/span>.<\/span>length<\/span>;<\/span>i<\/span>++<\/span>)<\/span>\r\n<\/span>{<\/span>\r\n  <\/span>document<\/span>.<\/span>write<\/span>(<\/span>x<\/span>[<\/span>i<\/span>]<\/span>.<\/span>childNodes<\/span>[<\/span>0<\/span>]<\/span>.<\/span>nodeValue<\/span>)<\/span>;\r\n  <\/span>document<\/span>.<\/span>write<\/span>(<\/span>\"<\/span>\"<\/span>)<\/span>;\r\n<\/span>}<\/span><\/pre>\n

\u8f93\u51fa\uff1a<\/p>\n

Everyday Italian\r\nHarry Potter\r\nXQuery Kick Start\r\nLearning XML<\/pre>\n

\u5b9e\u4f8b\u89e3\u91ca\uff1a<\/p>\n

    \n
  1. \u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n
  2. \u8bbe\u7f6e x \u53d8\u91cf\u6765\u4fdd\u5b58\u6240\u6709 title \u5143\u7d20\u7684\u8282\u70b9\u5217\u8868<\/li>\n
  3. \u4ece\u6240\u6709 <title> \u5143\u7d20\u7684\u6587\u672c\u8282\u70b9\u8f93\u51fa\u503c<\/li>\n<\/ol>\n
    DOM \u5c5e\u6027\u5217\u8868\uff08\u547d\u540d\u8282\u70b9\u56fe Named Node Map\uff09<\/strong><\/div>\n

    \u5143\u7d20\u8282\u70b9\u7684 attributes \u5c5e\u6027\u8fd4\u56de\u5c5e\u6027\u8282\u70b9\u7684\u5217\u8868\u3002<\/p>\n

    \u8fd9\u88ab\u79f0\u4e3a\u547d\u540d\u8282\u70b9\u56fe\uff08Named Node Map\uff09\uff0c\u9664\u4e86\u65b9\u6cd5\u548c\u5c5e\u6027\u4e0a\u7684\u4e00\u4e9b\u5dee\u522b\u4ee5\u5916\uff0c\u5b83\u4e0e\u8282\u70b9\u5217\u8868\u76f8\u4f3c\u3002<\/p>\n

    \u5c5e\u6027\u5217\u8868\u4f1a\u4fdd\u6301\u81ea\u8eab\u7684\u66f4\u65b0\u3002\u5982\u679c\u5220\u9664\u6216\u6dfb\u52a0\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5217\u8868\u4f1a\u81ea\u52a8\u66f4\u65b0\u3002<\/p>\n

    \u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u901a\u8fc7\u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d\uff0c\u5e76\u8fd4\u56de \"books.xml\" \u4e2d\u7b2c\u4e00\u4e2a<book> \u5143\u7d20\u7684\u5c5e\u6027\u8282\u70b9\u5217\u8868\uff1a<\/p>\n

    xmlDoc=loadXMLDoc(\"books.xml\");\r\n\r\nx=xmlDoc.getElementsByTagName('book')[0].attributes;<\/pre>\n

    \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u6267\u884c\u4e4b\u540e\uff0cx.length \u7b49\u4e8e\u5c5e\u6027\u7684\u6570\u91cf\uff0c\u53ef\u4f7f\u7528 x.getNamedItem() \u8fd4\u56de\u5c5e\u6027\u8282\u70b9\u3002<\/p>\n

    \u4e0b\u9762\u7684\u4ee3\u7801\u7247\u6bb5\u663e\u793a\u4e00\u4e2a book \u7684 \"category\" \u5c5e\u6027\u7684\u503c\uff0c\u4ee5\u53ca\u5176\u5c5e\u6027\u7684\u6570\u91cf\uff1a<\/p>\n

    \u5b9e\u4f8b<\/strong><\/p>\n

    xmlDoc=loadXMLDoc(\"books.xml\");\r\n \r\nx=xmlDoc.getElementsByTagName(\"book\")[0].attributes;\r\n \r\ndocument.write(x.getNamedItem(\"category\").nodeValue);\r\ndocument.write(\"\" + x.length);<\/pre>\n

    \u8f93\u51fa\uff1a<\/p>\n

    cooking\r\n1<\/pre>\n

    \u5b9e\u4f8b\u89e3\u91ca\uff1a<\/p>\n

      \n
    1. \u4f7f\u7528 loadXMLDoc() \u628a \"books.xml\" \u8f7d\u5165 xmlDoc \u4e2d<\/li>\n
    2. \u8bbe\u7f6e x \u53d8\u91cf\u6765\u4fdd\u5b58\u7b2c\u4e00\u4e2a <book> \u5143\u7d20\u7684\u6240\u6709\u5c5e\u6027\u7684\u4e00\u4e2a\u5217\u8868<\/li>\n
    3. \u4ece \"category\" \u5c5e\u6027\u8f93\u51fa\u503c<\/li>\n
    4. \u8f93\u51fa\u5c5e\u6027\u5217\u8868\u7684\u957f\u5ea6<\/li>\n<\/ol>\n
    5. \u8bbe\u7f6e x \u53d8\u91cf\u6765\u4fdd\u5b58\u7b2c\u4e00\u4e2a <book> \u5143\u7d20\u7684\u6240\u6709\u5c5e\u6027\u7684\u4e00\u4e2a\u5217\u8868<\/li>\n","protected":false},"excerpt":{"rendered":"

      \u5f53\u4f7f\u7528\u8bf8\u5982 childNodes \u6216 getElementsByTagName() \u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u65f6\uff0c\u4f1a\u8fd4\u56de\u8282\u70b9 […]<\/p>\n","protected":false},"author":1481,"featured_media":237701,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-237699","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=237699"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237699\/revisions"}],"predecessor-version":[{"id":237703,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237699\/revisions\/237703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/237701"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=237699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=237699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=237699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}