如果天等于,Javascript设置文本

时间:2017-02-09 09:11:08

标签: javascript

我的以下javascript应告诉我的客户何时会在特定日期收到他们的套餐,例如如果是星期天,我们将在星期一发送他们的包裹。

<script>
    function date() {
        if (new Date().getDay() == 5) {
            document.getElementById("date").innerHTML = "Order before <b>9pm</b> and receive your package <b>saturday</b>.";
            document.getElementById("date1").innerHTML = "<span style="color:#000; background-color:#FFFF03; padding:5px;">Same as above.</span>";
        }
        if (new Date().getDay() == 6) {
            document.getElementById("date").innerHTML = "Order <b>now</b> and we'll send your package <b>monday</b>.";
            document.getElementById("date1").innerHTML = "<span style="color:#000; background-color:#FFFF03; padding:5px;">Same as above.</span>";
        }
        if (new Date().getDay() == 0) {
            document.getElementById("date").innerHTML = "Order <b>now</b> and we'll send your package <b>monday</b>.";
            document.getElementById("date1").innerHTML = "<span style="color:#000; background-color:#FFFF03; padding:5px;">Same as above.</span>";
        }
    }
window.onload = date;
</script>
<span id="date"></span>

<span id="date1"></span>

但是,如果我将当天的号码改为今天,它就无法正常工作。

2 个答案:

答案 0 :(得分:0)

使用new Date().getDay(),您将获得当天的日期,如果您只想告诉客户他们的包裹将在第二天发货,那么只需将new Date().getDay()+1放入您的代码中!如果您的查询与我解释的不同,请告诉我!

答案 1 :(得分:0)

阻止代码运行的唯一方法是在双引号字符串中使用双引号导致语法错误。即,在Javascript中你不能这样做:

var myStr = "Hello "world", my name is Jamie";

你需要转义字符串,或者使用单引号作为字符串容器 - 这两个都可以工作:

var myStr = 'Hello "world", my name is Jamie';
// or
var myStr = "Hello \"world\", my name is Jamie";

这是您的代码正常工作(已将&#34; 0&#34;更改为&#34; 4&#34;以使其对今天作出反应)

&#13;
&#13;
function date() {
  if (new Date().getDay() == 5) {
    document.getElementById("date").innerHTML = "Bestil indtil kl. <b>21.00</b> og modtag din pakke <b>lørdag</b> til pakkeboks.";
    document.getElementById("date1").innerHTML = "<span style=\"color:#000; background-color:#FFFF03; padding:5px;\">Bestil indtil kl.   <b>21.00</b> og modtag din pakke <b>lørdag</b> til pakkeboks.</span>";
  }
  if (new Date().getDay() == 6) {
    document.getElementById("date").innerHTML = "Bestil <b>nu</b> og vi afsender <b>mandag</b>.";
    document.getElementById("date1").innerHTML = "<span style=\"color:#000; background-color:#FFFF03; padding:5px;\">Bestil <b>nu</b> og vi afsender <b>mandag</b>.</span>";
  }
  if (new Date().getDay() == 4) {
    document.getElementById("date").innerHTML = "Bestil <b>nu</b> og vi afsender <b>mandag</b>.";
    document.getElementById("date1").innerHTML = "<span style=\"color:#000; background-color:#FFFF03; padding:5px;\">Bestil <b>nu</b> og vi afsender <b>mandag</b>.</span>";
  }
}
window.onload = date;
&#13;
<span id="date">Bestil indtil kl. <b>21:00</b> i aften og modtag din pakke <b>i morgen</b>.</span>

<span id="date1">Bestil indtil kl. <b>21:00</b> i aften og modtag din pakke <b>i morgen</b>.</span>
&#13;
&#13;
&#13;