![](/img/trans.png)
[英]JavaScript - How to get the text of the first child of a DOM element?
[英]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.