【发布时间】:2011-01-14 13:26:06
【问题描述】:
我知道可以使用 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 进行了一些搜索,但没有运气。如果不出意外,我可能只是无法排除与 jQuery 对象的 attr() 方法相关的结果。提前致谢。
【问题讨论】: