我有以下 XML
<ProjectResponse xmlns="Services.Messages" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Projects xmlns:a="Services.DTO">
<a:Project>
<a:ID>113</a:ID>
<a:Name>Test project</a:Name>
<a:Documents>
<a:ProjectDocument>
<a:FileName>DS.docx</a:FileName>
<a:ID>65</a:ID>
<a:ProjectID>113</a:ProjectID>
</a:ProjectDocument>
</a:Documents>
</a:Project>
</Projects>
</ProjectResponse>
当我在“每个”函数中执行 $(this).find('[nodeName=a:ID]') 时,我得到 2 个 ID,一个来自 Project,另一个来自 Document。
$(projectsXml).find('Projects').children().each(function() {
var projectId = $(this).find('[nodeName=a:ID]').text();
问题是我如何才能只获得项目 ID,而不是文档 ID 和可能出现的其他 ID?
最佳答案
使用.children()
而不是 .find()
内部也是如此,所以它只寻找直接 child ,像这样:
$(projectsXml).find('Projects').children().each(function() {
var projectId = $(this).children('[nodeName=a:ID]').text();
});
关于javascript - 如何使用 jQuery 'find' 避免在带有命名空间的 xml 中使用相同标记的多个节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4303790/