【问题标题】:Solution to add className in specific Array element在特定 Array 元素中添加 className 的解决方案
【发布时间】:2019-05-31 01:02:13
【问题描述】:

我创建了一个日历,但没有找到当日期等于“今天”时添加一个className的解决方案,请看链接演示:

https://compassionate-beaver-fe6c05.netlify.com/

代码是:

https://github.com/fzabra/react-calendar/blob/master/src/Components/Calendar.jsx

以下条件返回今天的日期:

 if (dateDay === today.getDate() && currentYear === today.getFullYear() && currentMonth === today.getMonth()) {
   console.log(dateDay)   
}

观察控制台显示今天的日期。今天的广场是一个it元素,有一个id="dateDay",我认为像document.getElementById这样的东西不起作用,有人有什么想法吗?

谢谢

【问题讨论】:

  • 当前日期的样式在哪里?
  • '条件渲染'
  • 可以,但是没有css样式,dateDay是数字,加个today什么的
  • 我还没有创建样式,我只想在当天添加一个className。我在这个条件之间再次尝试了 push() 但它正在创建两个当前日期

标签: reactjs


【解决方案1】:

使用 CSS 文件中的 .box .green 类,您可以在 if 条件内使用 push <Table.Cell>else 不要使用 className,如下所示:

if (dateDay === today.getDate() && currentYear === today.getFullYear() && currentMonth === today.getMonth()) {
  children.push(<Table.Cell key={j} id={dateDay} className="box green">{dateDay}</Table.Cell>)
  } else{
  children.push(<Table.Cell key={j} id={dateDay} className="box">{dateDay}</Table.Cell>)

这样当前日期将具有绿色背景。
结果

Full code

【讨论】:

  • 对我来说,它创造了 2 个当前的日子,不适合你
  • 很好,现在我明白我哪里伤脑筋了哈哈,谢谢
猜你喜欢
  • 2021-07-31
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多