javascript - 根据 currentTime 更改类属性

标签 javascript html class variables time

所以,我有一些代码由于某些奇怪的原因而不起作用。它的任务是,更改 id 为 blogtxt 的元素的属性 class IF day 变量等于 4、5 或 6(周五、周六、周日) 我还希望在 day = 4 && 时使用 hours 变量。 另外,我确定问题不在 IF 语句中,因为代码甚至不会执行 alert(currentTime)

Q: What is the problem with the code above?

<script>
var currentTime = new Date()
var day = currentTime.getDay()
var hours = currentTime.getHours()
alert(currentTime)
if (day = 4 && || day = 5 || day = 6) {
    document.getElementById("blogtxt").setAttribute("class", "friss");
    else document.getElementById("blogtxt").setAttribute("class", "");
}
</script>

最佳答案

JSLint 检测到您有一堆错误:

day = 4 应为day == 4。 if 子句中有一个无关的 &&。 then 子句没有右大括号,else 子句没有左大括号。

已更正代码 http://jsfiddle.net/barmar/7nRXG/

关于javascript - 根据 currentTime 更改类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11448832/

相关文章:

javascript - 基于计时器 jQuery 循环悬停状态

jquery - 使用 height auto 获取绝对元素的高度

html - 一个人如何像这样布置 z-index ?

c++ - 如何在 C++ 中编写默认构造函数?

javascript - ListView HTML 静态与动态

javascript - Three.js 中心 3d 模型

javascript - 当输入类型日期和输入类型时间使用相同的ng-model时,如何清除仅日期或仅时间?

javascript - HTML 图像 onmouseover 事件未触发

c++ 指向函数的指针

python - 为比标准套牌中更复杂的扑克牌类型制作类(class)的好方法?