Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery获取Xml属性?_Javascript_Jquery_Xml - Fatal编程技术网

Javascript 如何使用jquery获取Xml属性?

Javascript 如何使用jquery获取Xml属性?,javascript,jquery,xml,Javascript,Jquery,Xml,我有以下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>re

我有以下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>

我想它打印出“层:1”

首先,您有不正确的选择器,用于将标记打开以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
});
是这样吗


请让我知道这是否有用。

大写字母不应matter@epascarello,你说得对。回答中更正。谢谢如果
openings
具有正确的选择器,则该代码应该可以工作……您还有另一个问题。显示更多上下文。
$('openings').each(function(j, opening_el)
{
  console.log("layers: " + $(opening_el).attr("layers")); //This is working
});
$("Openings").attr("layers")