2011-05-13 85 views
1

我想填充使用每個集合。我可以在一個字符串中獲取元素,並且每次迭代都可以獲取警報。我不能把兩者放在一起。填充集合()

這裏是我的清單:

2011/4/27下午5點42分54秒
2011/4/27下午五點43分00秒
2011/4/27下午5時46分53秒
2011/4/28 2:10:40 PM

這是我的代碼。

if (chosenTime > 0) { 
    var myDateClass = $('.exceptionDate') 
    var i = 0 
    $(myDateClass).each(function() { 
     var myDateClassVal = myDateClass[i].GetAttributeNode(outerText); 
     //this currently gives an error    
     //if I switch this to myDateClass.text(); 
     // it returns the list in one string 
     i++ 
     }); 

回答

2

裏面每次使用$(this).text()this是每個中的單個項目。

0

我想你感到困惑.each()

你可能想看看API:

http://api.jquery.com/each/

$(myDateClass).each(function() { 
     var myDateClassVal = myDateClass[i].GetAttributeNode(outerText); 
     i++; // This is not needed. 
     }); 

所以,如果你把它定義爲each(function(indx, elem) { })它會幫助你遍歷每個元素,elem,或通過索引引用它。

0

我不完全明白你的意思,但是如果你想要一個數組的每個.exceptionDate元素的文本內容,你可以使用.map().get()

var array = myDateClass.map(function() { 
    return $(this).text(); 
}).get(); 

或者以類似的方式,你可以使用jQuery.map()

var array = $.map(myDateClass,function (el) { 
    return $(el).text(); 
});