JavaScript - 关于数组中未定义变量的问题

时间:2014-02-05 13:01:34

标签: javascript php jquery arrays

嘿我很难解决这个问题: 我得到了一个php日历,并且每天都有一个订购ID,通过将其设置为“day + month”值(f.e, jan 5th #0501 )。我在这里尝试做的是使用javascript(以及 Jquery 来检测该ID,如果鼠标悬停在该广场上,则显示当天的事件。但是当我将鼠标悬停在广场上时,它会显示 “未定义”
使用Javascript:

var day = ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"];
var month = ["01","02","03","04","05","06","07","08","09","10","11","12"];

for (var j=0;j<=month.length;j++){
    for (var i=0;i<=day.length;i++){
        $("#" + day[i] + month[j]).hover(function(){
            $(this).css({'background-color':'#416E86'});
            $(".day-number-big").css({"background-color":"#EEE"});
            $(".day-number-big").html("<h4>" + day[i] + month[j] + "</h4>");
        },
        function(){
            $(this).css({'background-color':'#FFF'});
        });
    }
}

HTML&amp; CSS:

<div class="day-number-big" style="position:absolute;right:0px;width:75px;height:75px;background-color:#204F88;">
                    <div class="day-number-wrapper" style="position:static;width:23px;height:20px;margin:22px auto auto auto;color:#FFF;">

                    </div>
                </div>

PHP:

if ($list_day<10){
        $list_day="0".$list_day;
    }
    $sql = "SELECT * FROM cal_cma ORDER BY fec_cal";
    $query = mysql_query($sql,$conn);
    $checkdate= $list_day."".$month."".$year;
    while ($row = mysql_fetch_array($query)){
        $checkdate2 = date_adjust($row['fec_cal']);
          if ($checkdate == $checkdate2){
              $calendar.= '<td class="calendar-day" id="'.substr($checkdate2,0,4).'">';
              $calendar.= '<div class="day-number-db">'.$list_day.'</div>';
              $calendar.= "<p></p>";
              $calendar.= '</td>';
              break;
          }
    }
        if ($checkdate != $checkdate2){
              $calendar.= '<td class="calendar-day" id="'.substr($checkdate,0,4).'">';
              $calendar.= '<div class="day-number">'.$list_day.'</div>';
              $calendar.= "<p></p>";
              $calendar.= '</td>';
        }

注意:“date_adjust”功能将更改格式化为YYYY-MM-DD格式为DDMMYYYY。

1 个答案:

答案 0 :(得分:0)

初始化后, day [i]和month [j]将始终为“hover”事件中的day [day.length + 1]和month [month.length + 1]。

您应该从HTML元素的“id”属性“hover”回调中重新计算这些值。

另一种方法是将这些数据存储到HTML元素的jQuery对象中:

    $("#" + day[i] + month[j]).data( "month", month[j] );
    $("#" + day[i] + month[j]).data( "day", day[i] );

    $("#" + day[i] + month[j]).hover(function(){
                $(this).css({'background-color':'#416E86'});
                $(".day-number-big").css({"background-color":"#EEE"});
                $(".day-number-big").html("<h4>" + $(this).data( "day" ) + $(this).data( "month" ) + "</h4>");
            },
            function(){
                $(this).css({'background-color':'#FFF'});
            });