无法在循环中的最后一项上应用函数

时间:2016-01-05 15:33:21

标签: javascript jquery

colorMe是为每个用户着色匹配字符串的函数。我从这里调用colorMe函数:

 $(function()
{
    //alert("default");
    $('#contents').append("<table><tr>");
    var globalStore = {};
    globalStore.data = [];
    var data;
    $.when(
    // 1st query
    $.get('/search/show_all_tutors.php',function(data){globalStore.data = globalStore.data.concat(data);console.log(data)},"json")
).then(function() {

     var j=0;
     var plus; 
     var neg; 
     localStorage.size = globalStore.data.length;
    for (i = 0; i < globalStore.data.length; i++) {

 colorMe(globalStore.data[i].morning,globalStore.data[i].afternoon,globalStore.data[i].evening, globalStore.data[i].UUID,i);

<div class='small-6 medium-6 large-6 columns text-left day morning_"+globalStore.data[i].UUID+"' id='"+globalStore.data[i].UUID+"'>Morning: <span class='Mon'>M </span><span class='Tue'>T </span><span class='Wed'>W </span><span class='Thrs'>T </span><span class='Fri'>F </span><span class='Sat'>S </span><span class='Sun'>S</span></div><div class='small-6 medium-6 large-6 columns text-left day afternoon_"+globalStore.data[i].UUID+"' id='"+globalStore.data[i].UUID+"'>Afternoon: <span class='Mon'>M </span><span class='Tue'>T </span><span class='Wed'>W </span><span class='Thrs'>T </span><span class='Fri'>F </span><span class='Sat'>S </span><span class='Sun'>S</span></div><div class='small-6 medium-6 large-6 columns text-left day evening_"+globalStore.data[i].UUID+"' id='"+globalStore.data[i].UUID+"'>Evening: <span class='Mon'>M </span><span class='Tue'>T </span><span class='Wed'>W </span><span class='Thrs'>T </span><span class='Fri'>F </span><span class='Sat'>S </span><span class='Sun'>S</span></div>

...................................

这是colorMe功能

示例数据:

param = 'Sat,Sun';
param_af = 'Mon,Fri,Sat';
param_eve = '';
param2 = '13 digit user id';
index = denotes nth call to the function


    var userarray = [];
var dayarray  = [];
var dayarray2 = [];
var dayarray3 = [];
function colorMe(param,param_af,param_eve, param2,index)
{
var data = 0;   
    console.log("index= "+index);

    /*console.log("DAY "+param);
    console.log("user "+param2);*/
    //console.log("k= "+k);
    console.log("data= "+data);

    userarray.push(param2);
    //console.log("mY "+userarray[k]);
    dayarray.push(param);
    dayarray2.push(param_af);
    //console.log("afternoon "+dayarray2[k]);
    dayarray3.push(param_eve);
    //console.log("evening "+dayarray3[k]);
    //console.log("Day "+dayarray[k]);
    var k = userarray.length;
    console.log("K= "+k);

    for(;data < k;data++)
    {
        //console.log("data="+ data);
    var arr = dayarray[data];
    var arr2 = dayarray2[data];
    var arr3 = dayarray3[data];
    console.log("userdata= "+userarray[data]);
    $("div.morning_"+userarray[data]+" span").each(function() {
                if(arr.indexOf($(this).attr("class")) > -1) {

                  $(this).css("color","#26F525");

                }
            });


            $("div.afternoon_"+userarray[data]+" span").each(function() {
                if(arr2.indexOf($(this).attr("class")) > -1) {

                  $(this).css("color","#26F525");

                }
            });
            $("div.evening_"+userarray[data]+" span").each(function() {
                if(arr3.indexOf($(this).attr("class")) > -1) {

                  $(this).css("color","#26F525");

                }
            });

    }
    k++;
    //if(data == (localStorage.size-1)){
//alert("data"+data+" k= "+k+"size= "+localStorage.size+"user= "+param2+"morn= "+param+"aftr="+param_af+"eve="+param_eve);  

    //}
}

                }
  

如何在最后一项上应用colorMe功能?

     

缩小解释

     

我有一个包含10个项目的第一个循环。

     

调用colorMe函数的每个项目为其内容着色   绿色。

     

它假设为所有10个项目着色,但现在它只有颜色9   最后一个总是错过了。

     

现在如何让最后一项也被着色?

1 个答案:

答案 0 :(得分:1)

  • 每次循环都会增加k,而不会修改基于它的数组。
  • 您甚至不再设置 k,而是len。发布您的实际代码。
  • 代码中的重复数量令人难以置信。