根据时间在页面上打开或关闭业务文本 [英] Business Open/Closed text on page according to time

查看:54
本文介绍了根据时间在页面上打开或关闭业务文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定已经尝试了100种这种代码的变体,但似乎无法正确地做到这一点.我觉得我要在运动场上走得更远.

I must have tried a 100 variations of this code and can't seem to get it right. I feel like I'm walking further off the playing field.

我想在我们的页面上显示一条消息,以显示我们的公司是根据时间和/或日期而营业还是已营业.

I want to display a message on our pages that show whether our business is open or closed depending on time and/or day.

这是我当前的代码.

<script type="text/javascript">
var today = new Date()
var open = ("We're open today from 9am - 5pm</span>");
var closed = ("We're closed and will open again tomorrow 9am - 6pm</span>");
if (today == 0) display.innerHTML = 'closed';
if (today.getHours() >= 9 && today.getHours() < 18) {
    display.innerHTML = 'open';
} else {
    display.innerHTML = 'closed';
}
</script>

和我当前正在使用的HTML.

and the HTML I'm currently using.

<div><span id="display"></span></div>

推荐答案

尝试一下(请参见 jsfiddle ):

var today = new Date(),
    open = "We're open today from 9am - 5pm",
    closed = "We're closed and will open again tomorrow 9am - 6pm",
    display = document.getElementById('display');

if (today.getHours() >= 9 && today.getHours() < 18) {
    display.innerHTML = open;
} else {
    display.innerHTML = closed;
}

HTML:

<div id="display"></div>

我认为您的问题是它在 div 中显示为打开"或关闭",而此时应显示为我们今天上午9点至下午5点开放"或我们已关闭,将于明天上午9点至下午6点再次开放".问题是您将变量 open closed 引用为字符串.

I assume your problem was that it was displaying "open" or "closed" in the div, when it should be displaying either "We're open today from 9am - 5pm" or "We're closed and will open again tomorrow 9am - 6pm". The issue was that you were referencing the variables open and closed as strings.

这篇关于根据时间在页面上打开或关闭业务文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆