2010-02-08 21 views
41

我知道可以使用attr()方法检索单独的属性,但我试图遍历元素属性的所有。对于情况下,我使用了一些XML的jQuery ...用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... 
}); 

但我希望能够获得每个'项目'的属性数组,然后我可以迭代那些...

eg

$(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()方法有关的结果。提前致谢。

+1

见http://stackoverflow.com/questions/1705504/javascript-jquery-how-do-i-get-an-array-of-all-attributes-in-an- xml-element – 2010-02-08 21:22:50

回答

94

最好的方法是直接使用节点对象的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 :-) 
    }); 
}); 
+1

这样一个美丽的代码:)阅读好得多比经典的 – 2010-02-08 21:30:42

+0

谢谢,这个伎俩! – theraccoonbear 2010-02-09 14:44:37

+0

@prodigalson:感谢这种方法的索引foreach! – 2010-11-26 06:04:34

9

看来你必须使用普通的老香草的javascript:

for (var i = 0; i < elem.attributes.length; i++) { 
    var attrib = elem.attributes[i]; 
    if (attrib.specified == true) { 
     console.log(attrib.name + " = " + attrib.value); 
    } 
} 

How to iterate through all attributes in an HTML element?

+0

而这些都没有错。 attr()是一种方便的方法。 – 2010-02-08 21:25:09

0

怎么样?

$(xml).find('item').each(function() { 
    var attributes = $(this)[0].attributes; 
    for (attribute in attributes) { 
    // Do something with each attribute... 
    } 
}); 
3

您可以使用get()函数从jQuery包装中获取DOM元素,然后迭代DOM属性?

var node = $(myStuff).get(0); 
if (node.attributes.length) { 

    for (var length = attrs.length, i = 0; i < length; i++) { 
     if (attrs[i].specified) { 

     } 
    } 
} 

对于一些更强大的DOM属性处理,check this blog post

0

虽然你可以使用标准的DOM元素属性attributes,它将包括每个属性(即使那些未明确设置)在IE6中。作为替代方案,你可以限制你设置属性的数量:

var use_attributes = ['id','name','value','type']; 
$(xml).find('item').each(function() { 
    var $item = $(this); 

    $.each(use_attributes, function(){ 
    if($item.attr(this)){ 
     // Act on the attribute here. 
     // `this` = attribute name 
     // $item.attr(this) = attribute value 
    } 
    }) 
}); 
0

我张贴在这里,因为我觉得它可以帮助其他人在此张贴寻找解析XML文件,因为我是到达。

我正在寻找一种遍历xml文件的方法,它的结构与theracoonbear的结构非常相似,并将结果存储在一个数组中,并且发现了这篇文章。

我看着prodigitalson的代码,但我根本无法得到这个语法的工作 - 与Firefox抱怨说,在该行:

$.each(this.attributes, function(i, attrib){ 

这一点。属性

不是定义的函数。 我很确定这个错误完全是我的。但我已经花了几个小时试图得到这个工作,并且未能

什么工作对我来说(我的标记名称是会话,而不是项目): -

$(xml_Data).find("session").each(function() { 
    console.log("found session"); 
    $(this).children().each(function(){ 
    console.log("found child " + this.tagName); 
    console.log("attributes" + $(this).text()); 
    }); 
}); 

我认识到,这可能不完全回答原来的问题。不过,我希望这可能会让其他访问者能够节省一些时间。

问候

0
function findByAll(toLookFor, lookInText) { 
    return $('*').filter(function() { 
     return Array.prototype.some.call(this.attributes, function(attr) { 
      return attr.value.indexOf(toLookFor) >= 0; 
     }) || (lookInText && $(this).text().indexOf(toLookFor) >= 0); 
    }); 
}