Javascript 如何使用jquery获取Xml属性?
我有以下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
<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")