Javascript 如何使用jQuery从.each循环创建数组
如何从“.each循环”内部创建数组并在循环外部使用它 我的Javascript 如何使用jQuery从.each循环创建数组,javascript,jquery,Javascript,Jquery,如何从“.each循环”内部创建数组并在循环外部使用它 我的。每个循环: // 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).h
。每个循环:
// 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
创建数组?
那么如何使用这个数组呢?
e、 g
$(数组中的所有类作为选择器)。doStuff()代码>如果在每个
外部声明变量,则可以在每个
内部访问该变量:
var yourArray = [];
$('.profile-nav ul li a').not('.apply').each(function() {
if($(this).hasClass('cur')) {
yourArray.push($(this).prop('class').split(' ')[0]);
}
});
//Here, yourArray will contain the strings you require.
尽管如其他人所示,有很多方法可以显著缩短代码。您可以:
var list = $(".profile-nav ul li a.cur:not(.apply)");
list.each(function(){
// do your thing!
});
var arr = $( 'a.cur:not(.apply)', '.profile-nav' ).map( function () {
return $( this ).prop( 'class' ).split( ' ' )[0];
}).get();
fxnReqValidation=函数(){
var=新阵列;
InputAgarray=document.getElementsByTagName(“输入”);
对于(var iCnt=1;iCnt您将得到一个字符串数组。您想对这样一个数组做什么?jQuery方法是在包含DOM元素而不是字符串的数组上执行的。我希望使用字符串作为选择器来显示/隐藏不同分区中具有相同类的元素。过滤-排序。很好的示例!我以前从未使用过“.map”,我需要好的。如果OP-want是一个数组,不要忘记在.map()
之后链接.get()
或.toArray()
。
fxnReqValidation = function () {
var InputTagArray = new Array;
InputTagArray = document.getElementsByTagName("input");
for (var iCnt = 1; iCnt <= InputTagArray.length; iCnt++) {
if ((g_Json[InputTagArray[iCnt].name].required == true) && (InputTagArray[iCnt].value == "")) {
$("#errormsg").text("please enter all required fields");
}
return false;
}
}