【问题标题】:Getting attribute of xml child nodes in jQuery在jQuery中获取xml子节点的属性
【发布时间】:2016-08-01 19:22:00
【问题描述】:

我不得不修改这个问题,因为我遗漏了一段破坏事情的代码。我只想看某个教堂的图片。

xml 文件:

<churches>
<church>
        <data-name>germanevangelical</data-name>
        <name>German Evangelical</name>
        <address>501 Elm St.</address>
        <opened>1887</opened>
        <neighborhood>East</neighborhood>
        <region>East</region>
        <architecture>Gothic</architecture>
        <denomination>Evangelical Lutheran</denomination>
        <closed>2006</closed>
        <image caption="Mary Smith">image_1_forweb.jpg</image>
        <image caption="Mary Smith">image_2_forweb.jpg</image>
        <image caption="Mary Smith">image_3_forweb.jpg</image>
</church>
... (more church nodes)
</churches>

我想用 jQuery 访问图片标题。

这是我的代码,但它为字幕返回“未定义”:

var cName = 'germanevangelical';  
$.ajax({
              type: "GET",
              url: "churchdata.xml",
              dataType: "xml",
              success: function(xml) {
                var name = $(xml).find("data-name"); //get church names from xml file
                $(name).each(function(id, item) {
                    if ($(item).text() == cName) { //find the right church in the xml file
                        $(item).parent().find("image").each(function(id, node) {
                            console.log('id: ' + $(node).attr('caption'));//undefined
                        })
                    }//end if right church in xml file
                })
              }
            });

谁能看看我做错了什么?

【问题讨论】:

  • xml 是否已解析为 #document,其中 dataType 设置为 xml?还缺少关闭 ) .each()
  • 好吧,我是个白痴。我没有为我一直点击的教堂写属性。我对它上面的教会有属性。人为错误不容小觑....

标签: jquery xml attr


【解决方案1】:

在解析时,您的 ajax 代码中有一个非常小的错误,在查找“数据名称”时使用 $xml 而不是 $(xml)。请检查下面的代码..尝试在chrome中运行您的页面并检查开发人员工具中是否有任何错误,如果有任何错误它将无法工作。

var cName = 'germanevangelical';  
$.ajax({
type: "GET",
    url: "churchdata.xml",
    dataType: "xml",
    success: function (xml) {
    var xmlDoc = $.parseXML(xml),
    $xml = $(xmlDoc);
    var name = $xml.find("data-name"); //get church names from xml file
        $(name).each(function(id,item) {
              if ($(item).text() == cName) {
                $(item).parent().find("image").each(function(id,node) { 
                    console.log('id: ' + $(node).attr('caption')); //undefined
                });
               }
        });
      }
 });

【讨论】:

  • 这对结果没有影响,但谢谢。
  • 您可以尝试复制粘贴我的代码并尝试.. 您的代码中也几乎没有类型错误。
  • 你上面的答案有效,但是当我添加代码查看教堂名称是否匹配变量时,我无法再访问“图像”的属性。
  • 您的代码有效。我做了一件愚蠢的事,看错了没有字幕的教堂。
【解决方案2】:

$.parseXML() 不是必需的,其中$.ajax() 处的dataType 设置为"xml"XMLDocument 应在success$.ajax() 调用中返回为xml

另请注意,在问题时缺少关闭 ) .each() 呼叫 javascript

$.ajax({
  type: "GET",
  url: "churchdata.xml",
  dataType: "xml",
  success: function(xml) {
    console.log(xml);
    var name = $(xml).find("data-name"); //get church names from xml file
    $(name).each(function(id, item) {
      $(item).parent().find("image").each(function(id, node) {
        console.log('id: ' + $(node).attr('caption'));
      })
    })
  }
});

plnkrhttp://plnkr.co/edit/HYaaxN0EY0JtkciBMJpl?p=preview

【讨论】:

  • 这一直有效,直到我输入代码以查看教堂是否与我的变量匹配(见上文)。现在属性没有被返回。知道为什么吗?
  • @LauraNMS “这一直有效,直到我输入我的代码以查看教堂是否与我的变量匹配(见上文)。现在没有返回属性。知道为什么吗?” 你可以fork plnkr 来演示一下吗?似乎在 plnkr 版本 2 中返回预期结果
  • 问题是我没有在我的 xml 的那个节点中放置标题!愚蠢的人为错误。感谢您的帮助!
猜你喜欢
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多