如何使用JavaScript安排div显示?

时间:2013-02-26 12:10:00

标签: javascript

我有div id="button"我希望在工作日(周一至周五)上午9点到晚上19点播放。 如果未显示此div(“其他”),我想显示div id="different"

我想我在Show div based on getDay and getHours + getMinutes

找到了类似但更复杂的解决方案

问题在于我是JS中的假人而无法遵循。我们非常感谢一个简单易于调整的复制/粘贴解决方案!

1 个答案:

答案 0 :(得分:1)

这是一个更简单的解决方案:

var dt = new Date(); // get current date
var dy = dt.day(); // get the day from the date object
var hr = dt.getHours(); // get the hour from the date object

if (dy >= 1 && dy <= 5 && hr >= 9 && hr < 19) { // check the time is right
    document.getElementById('button').style.display = 'block'; // shows button as block
    document.getElementById('other').style.display = 'none'; // hides "other" button.
} else {
    document.getElementById('button').style.display = 'none'; // shows button as block
    document.getElementById('other').style.display = 'block'; // hides "other" button.
}

这非常具体地说明了你所说的内容,但可以参考this page进行调整,它将为你提供日期的数字(星期日是0,星期一,等等)以及JavaScript日期的其他属性如果你需要它们。

[我相信其他人也可以用它来玩code golf。]