繁体   English   中英

如何使用jquery获取Xml属性?

[英]How can I get an Xml attribute with jquery?

我有以下XML:

<Openings width="20" height="10" layers="1">
  <opening>
    <item>
      <x>1.5</x>
      <y>2.25</y>
      <width>3.5</width>
      <height>5.5</height>
      <type>rectangle</type>
    </item>
  </opening>
</Openings>

我有以下javascript代码:

$(openings).each(function(j, opening_el)
{
  console.log("layers: " + $(opening_el).attr("layers")); //This is not working
});

我希望它打印出“ layers:1”;

这有帮助

$("Openings").attr("layers")

请告诉我这是否有帮助。

首先,您使用不正确的选择器来定位带有标签打开位置的dom。 应该是$('Openings') 您还需要使用$(this)来访问.each()循环中的当前dom。

尝试这个:

 $('Openings').each(function(){
     console.log("layers: " + $(this).prop("layers")); 
 });

您在openings标签上调用jQuery时缺少引号,并使用this来获取循环中html元素的当前实例。

$('Openings').each(function(j, opening_el)
{
  console.log("layers: " + $(this).attr("layers"));
});

工作演示

是的,您已经错过了报价。 这个版本应该可以正常工作。

$('openings').each(function(j, opening_el)
{
  console.log("layers: " + $(opening_el).attr("layers")); //This is working
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM