Javascript 如何在日历中设置单击日期的样式?
我使用的是react日历,我试图在点击某个特定日期后设置其样式,但我只找到了如何设置整个月份的样式。我试着研究是否可以将在div中单击的日期包装起来,并将该div分配给一个类,但我找不到这样做的方法。还有其他想法吗Javascript 如何在日历中设置单击日期的样式?,javascript,reactjs,Javascript,Reactjs,我使用的是react日历,我试图在点击某个特定日期后设置其样式,但我只找到了如何设置整个月份的样式。我试着研究是否可以将在div中单击的日期包装起来,并将该div分配给一个类,但我找不到这样做的方法。还有其他想法吗 export const CalendarPage = () => { const datesToAddClassTo = ["Thu May 06 2021 00:00:00 GMT-0400 (Eastern Daylight Time)"];
export const CalendarPage = () => {
const datesToAddClassTo = ["Thu May 06 2021 00:00:00 GMT-0400 (Eastern Daylight Time)"];
function tileClassName({date, view) {
// Check if a date React-Calendar wants to check is on the list of dates to add class to
if (view === 'month') {
if (datesToAddClassTo[0] === date.toString()) {
console.log("Match found!!!!!");
return 'myClassName';
}
}
}
const [value, setValue] = useState(new Date());
function onChange(nextValue) {
setValue(nextValue);
}
return (
<Calendar
onChange={onChange}
value={value}
tileClassName={tileClassName}
/>
);
export const CalendarPage=()=>{
const datesToAddClassTo=[“2021年5月6日星期四00:00:00 GMT-0400(东部夏时制)”;
函数tileClassName({日期,视图){
//检查要检查的日期是否在要添加类的日期列表中
如果(视图=='month'){
如果(datesToAddClassTo[0]==date.toString()){
日志(“找到匹配项!!!!!!!”;
返回“myClassName”;
}
}
}
const[value,setValue]=useState(新日期());
函数onChange(nextValue){
设置值(下一个值);
}
返回(
);
尝试更改以下内容:
function tileClassName(data) {
const { _, date, view } = data;
// Check if a date React-Calendar wants to check is on the list of dates to add class to
if (view === 'month') {
if (datesToAddClassTo[0] === date.toString()) {
console.log("Match found!!!!!");
return 'myClassName';
}
}
}
然后为
.myClassName
添加样式,如果它不适用,则添加-add!important
属性。嘿,你应该避免!important
属性,因为它被认为是反模式的。下面是如何避免这种情况。