我知道可以使用该attr()
方法检索单个属性,但我正在尝试遍历元素的所有属性。对于上下文,我在一些 XML 上使用 jQuery ......
<items>
<item id="id123" name="Fizz" value="Buzz" type="xyz">
<subitem name="foo">
<subitem name="bar">
</item>
<item id="id456" name="Bizz" value="Bazz" type="abc">
<subitem name="meh">
<subitem name="hem">
</item>
</items>
我已经能够迭代这些项目......
$(xml).find('item').each(function() {
// Do something to each item here...
});
但我希望能够为每个“项目”获取一组属性,这样我就可以遍历那些......
例如
$(xml).find('item').each(function() {
var attributes = $(this).attributes(); // returns an array of attributes?
for (attribute in attributes) {
// Do something with each attribute...
}
});
我在这里、jQuery 文档和其他地方通过 Google 进行了一些搜索,但没有运气。如果不出意外,我可能只是无法排除与attr()
jQuery 对象的方法相关的结果。提前致谢。