簡體   English   中英

在javascript中獲取xml dom子元素

[英]Get xml dom child element in javascript

我有一個這樣的XML文檔:

<section>
   <description></description>
   <question>
       <description></description>
   </question>
</section>

我已經將sectionXML存儲在名為SectionXML的變量中。 我用:

section.description = SectionXML.getElementsByTagName("description")[0].childNodes[0];

section.description = section.description ? section.description.nodeValue : "";

但是, SectionXML.getElementsByTagName("description")返回許多描述的列表,就好像我們更深入地看了許多子項(偉大的大子項等)一樣,我們可以看到它們都有描述,但是我只想獲取SectionXML的直接子項標簽名稱為“ description”。 我怎么做?

您可以使用querySelectorAll():

SectionXML.querySelectorAll("section>description")[0]
// use > to get direct element only

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM