【问题标题】:Ajax/XML Parsing - How can I get only the parent's attribute when the child has the same attr?Ajax/XML Parsing - 当孩子具有相同的属性时,如何仅获取父属性?
【发布时间】:2013-01-04 04:33:58
【问题描述】:
<parent>
  <name></name>
  <color></color>
    <child>
      <name></name>
    </child>
</parent>

如何只获取父名而不获取子名?

到目前为止,我有这个,但它返回两个名称属性。

function parseXml(xml)
{
  $(xml).find("parent").each(function()
  {
    $("#id").append($(this).find("name").text()+ "<br />");
  });
}

【问题讨论】:

    标签: jquery xml ajax parsing


    【解决方案1】:

    使用children() 代替 find()

    function parseXml(xml)
    {
      $(xml).find("parent").each(function()
        {
             $("#id").append($(this).children("name").text()+ "<br />");
        });
     }
    

    更新

    这将删除子文本并仅返回父文本

    $(xml).find("parent").each(function()
    {
         alert($(this).clone().find("child").remove().end().text()+ "<br />");
    });​
    

    Working Demo

    【讨论】:

    • 但我不想要子属性 - 只有父属性。
    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 2023-01-27
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多