简体   繁体   中英

Need to add events to calendar

I currently have code that creates a monthly calendar:

 var i = 1;

 do{

     var writeDays = new Date(year, month, i).getDay();
     //if it's Sunday, start a new row
     if(writeDays == 0)
     {
         html += '<tr>';
     }
     //if it's not Sunday but the first day of the month, write the last days from the previous month
     else if(i == 1)
     {
         html += '<tr>';
         var count = lastDayofPreviousMonth - firstDayofMonth + 1;
         for(var j = 0; j < firstDayofMonth; j++)
         {
             html += '<td class = "not-current">' + count + '</td>';
             count++;

         }
     }

     html += '<td class = "hover">' + i  + '</td>';


     //if it's Saturday, ends the row
     if(writeDays == 6)
     {
     html += '</tr>';
     }
     //if it isn't Saturday, but last day of the current month, write the next few days from the next month
     else if(i == lastDayofMonth)
     {
     var count = 1;
     for(writeDays; writeDays < 6; writeDays++)
     {
     html += '<td class = "not-current">' + count + '</td>';
     count++;
     }
     }
     i++;
 }
 while(i <= lastDayofMonth);

I want to be able click on a day (cell) in the array and add an event to the day. I'm not too sure how to implement this. Any help would be appreciated!

you can add an eventListener to the table element. for example.I have a table dom like this,which I believe is similiar to your dom structure:

<table id='tb'>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
 </table>

and I want every td element can respone to a click event,so I can do this:

var table=document.getElementById('tb');
table.addEventListener('click',function(e){
  console.log(e);
});

in parameter e,you can get a lot information.just try it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM