我有一个这样的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的直接孩子标签名称为&#34; description&#34;。我该怎么做?
答案 0 :(得分:0)
您可以使用querySelectorAll():
SectionXML.querySelectorAll("section>description")[0]
// use > to get direct element only