如何在FullCalendar中更改所选日期的背景色

时间:2019-03-24 11:21:33

标签: javascript css fullcalendar fullcalendar-3

我正在尝试更改日历中所选日期的背景颜色。在下面的代码中,它突出显示了所有单击的日期,如何仅突出显示上次单击的日期?

dayClick: function (day){
  var mydate = new Date(this[0].getAttribute("data-date"));
  var dateArray = mydate.toDateString().split(" ");
  document.querySelectorAll(".calendar-mobile p")[0].innerHTML = j_monthNames[mydate.getMonth()] + " " + mydate.getDate() + "日" + "(" + j_dayNames[dateArray[0].toLocaleLowerCase()] + ")";
  document.body.classList.remove("calendar-open");

  $month = '' + (mydate.getMonth() + 1);
  $day = '' + mydate.getDate();
  $year = mydate.getFullYear();

  if ($month.length < 2) $month = '0' + $month;
  if ($day.length < 2) $day = '0' + $day;
  $dates = [$year, $month, $day].join('-');
  $('[data-date='+$dates+']').css({"color": "red", "backgroundColor": "yellow",});
},

3 个答案:

答案 0 :(得分:3)

您已经使这种方式变得比所需的复杂。

只需定义一个CSS类,即可将其添加到当天的HTML元素中时设置您希望的颜色。

然后,在dayClick函数中,首先找到所有具有该类的元素,然后从中删除该类。这将阻止它在以前点击的日期显示。

接下来,将类添加到当前元素(由this表示)。简单!

JS:

dayClick: function (day){
  $(".day-highlight").removeClass("day-highlight");
  $(this).addClass("day-highlight");
}

CSS:

.day-highlight {
  background-color: yellow !important;
  color: red !important;
}

({!important对于覆盖由fullCalendar自动设置的当前日期的颜色突出显示是必需的。)

实时演示:http://jsfiddle.net/zs9g5a8k/

答案 1 :(得分:2)

我终于以这种方式解决了它:

$(document).ready(function() {
  $('#calendars').fullCalendar({
    header: {
      left: 'prev',
      center: 'title',
      right: 'next'
    },
    selectable: true,
  });
});
.fc-highlight {
  background: green !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.0.1/fullcalendar.min.css" defer/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.0.1/fullcalendar.min.js" defer></script>
<div id="calendars"></div>

只要触发了.fc-highlight类,它就可以解决问题。

答案 2 :(得分:0)

如果有人想知道如何使用 Angular2 +

<full-calendar #calendar
               id="calendar"
               (dateClick)="handleDateClick($event)"
               deepChangeDetection="true"
               defaultView="dayGridMonth"
               [locales]="[itLocale]"
               locale="it"
               [selectable]="true"
               [header]="options.header"
               [plugins]="calendarPlugins">
</full-calendar>

打字稿文件中:

import { Component, OnInit, ViewChild } from '@angular/core';
import dayGridPlugin from '@fullcalendar/daygrid';
import { FullCalendarComponent } from '@fullcalendar/angular';
import itLocale from '@fullcalendar/core/locales/it';
import interactionPlugin from '@fullcalendar/interaction';

interface iDay {
  dayLabel: string,
  dayMs: number
}
@Component({
  selector: 'app-calendar',
  templateUrl: './calendar.component.html',
  styleUrls: ['./calendar.component.scss']
})
export class CalendarComponent implements OnInit {

  @ViewChild('calendar') calendarComponent: FullCalendarComponent;

  public itLocale = itLocale;
  public calendarPlugins = [interactionPlugin, dayGridPlugin];
  public calendarEvents = [];
  public options = {
    header: {
      left: 'prev title',
      right: 'next'
    }
  }

  constructor() { }

  ngOnInit(): void {

  }

  handleDateClick(event) {
    document.querySelectorAll("td[data-date='" + event.dateStr + "']").forEach((nodeElement: HTMLElement) => {
      if(nodeElement.classList.value.includes('fc-day-top')){
        nodeElement.classList.add('selected-day');
      }
    })
  }

}