【问题标题】:How to use jquery to find xml element with ':' in name? [duplicate]如何使用 jquery 查找名称中带有 ':' 的 xml 元素? [复制]
【发布时间】:2014-12-12 06:45:19
【问题描述】:

我正在尝试使用 jquery 来解析并找到一个 xml 元素。我试图找到的元素有一个奇怪的名称“ex:relatedid”,出于某种原因,jquery 似乎无法找到它。有任何想法吗?提前致谢!

XML

    <item>
        <id>3ac0daff-51c7-40f2-9396-e0c6adf50858</id>
        <published>2014-12-09T11:27:43.000Z</published>
        <updated>2014-12-09T11:27:43.000Z</updated>
        <ex:relatedid>7fa16377-3382-4ad9-ac52-7ee139e7d9ce</relatedid>

不工作的jquery

 $(xml).find('item').each(function () {

          //works
          var id = $(this).find('id').text();

          //doesn't work
          var ex = $(this).find('ex:relatedid').text();

          console.log(ex);

   });

【问题讨论】:

  • 转义$(this).find('ex\\:relatedid')

标签: javascript jquery xml


【解决方案1】:

这样它会起作用,您需要转义“字符”,如“:”

DEMO

$('xml').find('item').each(function () {

      //works
      var id = $(this).find('id').text();

      //doesn't work
      var ex = $(this).find('ex\\:relatedid').text();

      console.log(ex);

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2011-06-04
    • 2010-12-20
    • 2011-08-13
    相关资源
    最近更新 更多