将元素数组传递给jquery/javascript函数

将元素数组传递给jquery/javascript函数,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我正在尝试用html和jquery/javascript制作一个日历。在这个日历中,我试图用一个给定的类在一行div中动态显示日期,该类在另一个div中浮动 HTML格式如下: <div id="view-month"> <div class="day">&nbsp;&nbsp;</div> ......an array of 42 elements for a 7 x 6 grid for viewing a month

我正在尝试用html和jquery/javascript制作一个日历。在这个日历中,我试图用一个给定的类在一行div中动态显示日期,该类在另一个div中浮动

HTML格式如下:

<div id="view-month">

    <div class="day">&nbsp;&nbsp;</div>
    ......an array of 42 elements for a 7 x 6 grid for viewing a month
    <div class="day">&nbsp;&nbsp;</div>
    <div class="day">&nbsp;&nbsp;</div>

</div>

……一个由42个元素组成的阵列,用于7 x 6的网格,用于一个月的查看
我正在尝试的jquery如下所示:

        $(document).ready(  function() { 

        var date = new Date(); // Gets the current date in a variable
        var selected_date = date.getDate(); // gets the current date (only dd)
        var month_no = date.getMonth(); // Gets the month as number - like January : 0, February : 1, March : 2
        var year = date.getFullYear(); // Gets the year in 'yyyy' format
        var month_string; // variable for holding the no. of days in the selected month
        var no_of_days_in_month; // variable for holding the total no. of days in the selected month for the selected year

        // ******** The following array holds the names of all the months for showing the chose month ********* //
        var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

        $('#month-and-year').html(months[month_no] + ', ' + year); // shows the month in the heading of the calendar on month-name, 'yyyy' format


        // ******** code for showing dates of the selected month and year showing in the header ********//

        //function monthDetail(month_string) { }

        if (month_string == 'January') {
            var no_of_days_in_month = 31;
            month_no = 0;
        }

        if (month_string == 'February') {

            var isleapyear = function(year) {
                return (yr % 400) ? ((yr % 100) ? ((yr % 4) ? false : true) : false) : true;
            }

            if (isleapyear) {
                var no_of_days_in_month = 29;
            }

            else {
                var no_of_days_in_month = 28;
            }
            month_no = 1;
        }

        if (month_string == 'March') {
            var no_of_days_in_month = 31;
            month_no = 2;
        }

        if (month_string == 'April') {
            var no_of_days_in_month = 30;
            month_no = 3;
        }

        if (month_string == 'May') {
            var no_of_days_in_month = 31;
            month_no = 4;
        }

        if (month_string == 'June') {
            var no_of_days_in_month = 30;
            month_no = 5;
        }

        if (month_string == 'July') {
            var no_of_days_in_month = 30;
            month_no = 6;
        }

        if (month_string == 'August') {
            var no_of_days_in_month = 31;
            month_no = 7;
        }

        if (month_string == 'September') {
            var no_of_days_in_month = 30;
            month_no = 8;
        }

        if (month_string == 'October') {
            var no_of_days_in_month = 31;
            month_no = 9;
        }

        if (month_string == 'November') {
            var no_of_days_in_month = 30;
            month_no = 10;
        }

        if (month_string == 'December') {
            var no_of_days_in_month = 31;
            month_no = 11;
        }

        var alldivs = $('.day');  // the entire predfined array of divs with a given number (42, in the present case)
                                  // (for a calendar - will be depending on the design of the grid for the month-view ) 
                                  // is assigned to a variable


            // ********* the following function draws / shows the calendar for the chosen month and year *********** //

            function showCalendar(year, month_no, selected_date, alldivs) { 

                // ************** Get the first day of the month showing on the top pane **************//

                var FirstDayofMonth = new Date(year, month_no, 1); // returns the first day of the given month in long date format 
                                                               // showing the day of the week, date, month, year and time

                // ************** Get the first day of the first week of the month showing on the top pane **************//

                var FirstDayofFirstWeek = FirstDayofMonth.getDay(); // returns the number place of the first day of the month
                                                                // within 0 to 6 i.e. 7 days of week

                var ofset; // this variable holds the number of divs to be left free from top left while showing the month selected

                ofset = FirstDayofFirstWeek;

                var no_of_div; // no of divs to be highlighted as days of the month in concern

                no_of_div = no_of_days_in_month;



                // **** the following variable holds the no of divs from the first div in the matrix of the calendar to the div 
                // **** showing the last date of the month in concern **** //

                var divcount = parseInt(ofset)+parseInt(no_of_div); //without parseInt() it will produce garbage and hence ridiculously wrong result


                var i; //index for looping over the entire array of divs ( the entire grid for a calendar)  

                var j = 1; //for counting date of a calendar



                for (i = 0; i <= divcount-1; i++) {

                        if (i >= ofset && i <= divcount) {

                            $(alldivs[i]).html(j).css({'background-color':'#ff7'});

                                if (j == selected_date) {

                                    $(alldivs[i]).html(j).css({'background-color':'#def'});

                                }

                        j++;}
                    }

                } // end of function showCalendar()


        // ******************* end of function showCalendar() ********************** //


        showCalendar(year, month_no, selected_date, alldivs);

        // ******** code to show calendar in the grid ******** //

        $('#go-to-previous-year').on('click', function() {
            year = year - 1;
            $('#month-and-year').html(months[month_no] + ', ' + year);
            showCalendar(year, month_no, selected_date, alldivs );
        });

        $('#go-to-next-year').on('click', function() {
            year = year + 1;
            $('#month-and-year').html(months[month_no] + ', ' + year);
            showCalendar(year, month_no, selected_date, alldivs );
        });

        $('#go-to-previous-month').on('click', function() {

            if (month_no == 0) { 
                month_no = 12;
                month_no = month_no - 1;
                $('#month-and-year').html(months[month_no] + ', ' + year);
                month_string = months[month_no];
                showCalendar(year, month_no, selected_date, alldivs );
            }
             else {
                month_no = month_no - 1;
                $('#month-and-year').html(months[month_no] + ', ' + year);
                month_string = months[month_no];
                showCalendar(year, month_no, selected_date, alldivs );
            }
        });

        $('#go-to-next-month').on('click', function() {
            if (month_no == 11) { 
                month_no = -1;
                month_no = month_no + 1;
                $('#month-and-year').html(months[month_no] + ', ' + year);
                month_string = months[month_no];
                showCalendar(year, month_no, selected_date, alldivs );
            }
            else {
                month_no = month_no + 1;
                $('#month-and-year').html(months[month_no] + ', ' + year);  
                month_string = months[month_no];
                showCalendar(year, month_no, selected_date, alldivs );              
            }

        });

        }); //end of $(document).ready()
$(文档).ready(函数(){
var date=new date();//获取变量中的当前日期
var selected_date=date.getDate();//获取当前日期(仅dd)
var month_no=date.getMonth();//以数字形式获取月份,如一月:0、二月:1、三月:2
var year=date.getFullYear();//以“yyyy”格式获取年份
var month_string;//用于保存所选月份天数的变量
var no_of_days_in_month;//用于保存所选年份中所选月份的总天数的变量
//*******以下数组包含显示所选月份的所有月份的名称*******//
var月数=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’];
$(“#月和年”).html(月[月号]+”,“+年);//以月份名称“yyy”格式在日历标题中显示月份
//*******用于在标题中显示所选月份和年份的日期的代码********//
//函数monthDetail(month_字符串){}
如果(月份字符串=='一月'){
月内各天的风险值=31;
月号=0;
}
如果(月份字符串=='二月'){
var isleapyear=函数(年){
回报率(年%400)?((年%100)?((年%4)?假:真):假:真;
}
如果(每年){
月内各天的风险值=29;
}
否则{
月内各天的风险值=28;
}
第1个月;
}
如果(月份字符串=='March'){
月内各天的风险值=31;
第2个月;
}
如果(月份字符串=='April'){
月内各天的风险值=30;
第3个月;
}
如果(月字符串=='May'){
月内各天的风险值=31;
第4个月;
}
如果(月份字符串=='June'){
月内各天的风险值=30;
第5个月;
}
如果(月份字符串=='July'){
月内各天的风险值=30;
第6个月;
}
如果(月份字符串=='August'){
月内各天的风险值=31;
第7个月;
}
如果(月\字符串=='九月'){
月内各天的风险值=30;
第8个月;
}
如果(月\字符串==“十月”){
月内各天的风险值=31;
第9个月;
}
如果(月份字符串=='11月'){
月内各天的风险值=30;
第10个月;
}
如果(月份字符串=='December'){
月内各天的风险值=31;
第11个月;
}
var alldivs=$('.day');//具有给定数字的整个预先定义的div数组(在本例中为42)
//(对于日历-将取决于月视图网格的设计)
//分配给一个变量
//*******以下函数绘制/显示所选月份和年份的日历**********//
函数showCalendar(年、月、所选日期、所有divs){
//*************获取顶部窗格上显示的每月第一天**************//
var FirstDayofMonth=新日期(年、月);//以长日期格式返回给定月份的第一天
//显示星期几、日期、月份、年份和时间
//*************获取顶部窗格中显示的每月第一周的第一天**************//
var FirstDayofFirstWeek=FirstDayofMonth.getDay();//返回当月第一天的数字和位置
//在一周的0到6天内,即7天内
var of set;//此变量保存在显示所选月份时要从左上角保留的div数
ofset=firstdayOFFStweek;
var no_of_div;//要突出显示为相关月份天数的div数
无天数=月内无天数;
//****以下变量保存日历矩阵中第一个div到div的div数
//****显示相关月份的最后日期****//
var divcount=parseInt(of set)+parseInt(no_of_div);//如果没有parseInt(),它将产生垃圾,从而产生可笑的错误结果
var i;//用于在整个div数组(日历的整个网格)上循环的索引
var j=1;//用于计算日历的日期

for(i=0;i=of set&&i
month\u字符串
未在任何位置设置,因此
no\u of \u days\u在\u month
中从未设置,因此所有内容都会被忽略


修好这些,我至少得到一个黄色的大盒子,上面有1-31页和16页(今天)突出显示,而不是空白页。我不确定最终结果是否应该是这样,但这是向前迈出的一步。

我的第一个问题是,您是否在每次为其赋值时都有意重新声明“月天数”为“否”?因为您在顶部声明了该值,并在整个过程中重新声明了该值。好的,这可以取消……是的.因割伤和割伤而造成的匆忙失误