我有li
元素的類名稱爲c_on
,我需要得到所有這li
的屬性,並需要推入數組。爲此,我確實喜歡這樣,但沒有結果,任何人都知道這樣做的好方法?jquery如何使用find來推送數據?
var pushEmpl = [];
pushEmpl.push($('.demographics').find('ul.ocptn').find($('li.c_on')).each(function(){ return $(this).attr('data-ocp')}));
但它沒有得到結果。任何其他方式推動呢?
如何推動唯一值?有什麼辦法嗎? (li.c_on不重複的唯一值) – 3gwebtrain 2012-04-11 14:45:22
使用'.indexOf()'檢查值是否已經在數組中。例如'if(pushEmpl.indexOf($(this).attr('data-ocp'))== -1){pushEmpl.push($(this).attr('data-ocp')); }'參考:http://www.tutorialspoint.com/javascript/array_indexof.htm – AndrewR 2012-04-12 21:54:18