为了账号安全,请及时绑定邮箱和手机立即绑定

为什么这个简单的(嵌套的)if 语句不起作用?

为什么这个简单的(嵌套的)if 语句不起作用?

慕码人2483693 2023-04-14 16:37:35
我希望实现以下目标:如果是星期六或星期日,则显示我们已关闭页面。如果不是星期六或星期日:.如果在工作时间以外(不是上午 9 点到中午 12 点之间),显示我们已关闭页面。.如果是在工作时间内,显示我们开放的页面。这是代码:<html><head><TITLE>Golborne Patient Booking Portal </TITLE><script language="JavaScript" type="text/javascript">function ampmRedirect() {       var currentTime = new Date();    var currentHour = currentTime.getHours();    var d = new Date();    var n = d.getDay();        if (n == 0) || (n == 6){                                                    // if Saturday or Sunday        window.location.href = "closed.html";                                   // we are closed        } else {                                                                // if it is any other day            if ((currentHour < 8) || (currentHour > 11)) {                      // if outside of working hours (8am to noon)                window.location.href = "closed.html";                           // we are closed            } else {                                                            // anything else (not weekday or within the times)                window.location.href = "open.html";    // we are open            }    }}</script></head><body onload="ampmRedirect()"></body></html>我哪里错了?
查看完整描述

1 回答

?
开满天机

TA贡献1786条经验 获得超12个赞

除了条件表达式周围缺少的括号外,您还可以对日期和时间进行一次检查,然后分配一个结束页面或另一个目标。


function ampmRedirect() {

    var date = new Date(),

        time = date.getHours(),

        day = date.getDay();


    window.location.href = day === 0 || day === 6 || time < 8 || time > 11

        ? "closed.html"

        : "https://golborne.abtrace-cloud.com";

}

<body onload="ampmRedirect()">


查看完整回答
反对 回复 2023-04-14
  • 1 回答
  • 0 关注
  • 60 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信