jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用jQuery迭代元素属性大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道单个属性可以用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...
});

但我想能够得到一个属性数组的每个’项目’,@R_31_9447@重复这些…

例如

$(xml).find('item').each(function() {
  var attributes = $(this).attributes(); // returns an array of attributes?
  for (attribute in attributes) {
    // Do something with each attribute...
  }
});

在这里做了一些搜索,在jQuery文档,和其他地方通过谷歌,但没有运气。如果没有别的,我可能只是有麻烦排除与jQuery对象的attr()方法相关的结果。提前致谢。

解决方法

最好的方法通过使用其attributes属性直接使用节点对象。在我的解决方案下面的唯一区别与其他人建议这种方法是,我会再次使用.each而不是传统的js循环:
$(xml).find('item').each(function() {
  $.each(this.attributes,function(i,attrib){
     var name = attrib.name;
     var value = attrib.value;
     // do your magic :-)
  });
});

大佬总结

以上是大佬教程为你收集整理的使用jQuery迭代元素属性全部内容,希望文章能够帮你解决使用jQuery迭代元素属性所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。