【发布时间】:2011-12-06 21:07:16
【问题描述】:
如何从“.each 循环”内部创建一个数组并在循环外部使用它?
我的.each loop:
// Loop through all but button with class .apply
$('.profile-nav ul li a').not('.apply').each( function() {
// if currently loop through element has .cur class
if( $(this).hasClass('cur') ) {
//Get the first class of the match element
var ClassesToApply = $(this).prop('class').split(' ')[0];
}
//How can I create an array from all ClassesToApply?
//var arr = jQuery.makeArray(ClassesToApply);
// This will create an array, but with one element only
});
如何从所有var = ClassesToApply 创建一个数组?
我该如何处理这个数组呢? 例如
$( allClasses from an array as a selectors).doStuff();
【问题讨论】:
-
你会得到一个字符串数组。你想用这样的数组做什么? jQuery 方法在包含 DOM 元素的数组上执行,而不是字符串。
-
我希望使用字符串作为选择器来显示/隐藏在不同 div 中具有相同类的元素。过滤 - 有点。
标签: javascript jquery