show/hide di tiga waktu yang berbeda javascript

membuat show/hide

- show only 12.00 -14.00 id "malam" show, other id hide.

- show only 17.00 - 03.00 id "pagi" show, other id hide.

- show 07.00 - 10.00 span "siang" show, other id hide

<span id="malam" style="background-color:magenta;"> malam </span>
<br/>
<span id="pagi" style="background-color:cyan;"> pagi </span>
<br/>
<span id="siang" style="background-color:orange;"> siang </span>

<script>
            var start = new Date();
			var end = new Date();
            var time = new Date().getTime();

            if (time > start.setHours(12,00) && time < end.setHours(14,00)){
                document.getElementById("malam").style.display = "block";
                document.getElementById("pagi").style.display = "none";
                document.getElementById("siang").style.display = "none";
            }
            else if (time > start.setHours(07,00) && time < end.setHours(10,00)){
                document.getElementById("malam").style.display = "none";
                document.getElementById("pagi").style.display = "block";
                document.getElementById("siang").style.display = "none";
            }

                else (time > start.setHours(17,00) && time < end.setHours(03,00)){
                document.getElementById("malam").style.display = "none";
                document.getElementById("pagi").style.display = "none";
                document.getElementById("siang").style.display = "block";
            }

else  {
                document.getElementById("malam").style.display = "none";
                document.getElementById("pagi").style.display = "none";
                document.getElementById("siang").style.display = "none";
            }



avatar fauzanrozaq
@fauzanrozaq

1 Kontribusi 1 Poin

Diperbarui 3 tahun yang lalu

1 Jawaban:

<div>coba kodenya dibuat seperti ini:&nbsp;</div><pre>&lt;span id="malam" style="background-color:magenta;"&gt; malam &lt;/span&gt; &lt;br/&gt; &lt;span id="pagi" style="background-color:cyan;"&gt; pagi &lt;/span&gt; &lt;br/&gt; &lt;span id="siang" style="background-color:orange;"&gt; siang &lt;/span&gt;

&lt;script&gt; var pagi = document.getElementById("pagi") var siang = document.getElementById("siang") var malam = document.getElementById("malam") var time = new Date().getHours();

if(time &amp;gt;= 12 &amp;amp;&amp;amp; time &amp;lt;= 14){
    malam.style.display = &quot;block&quot;;
    pagi.style.display = &quot;none&quot;;
    siang.style.display = &quot;none&quot;;
}

else if(time &amp;gt;= 17 || time &amp;lt;= 3){
    malam.style.display = &quot;none&quot;;
    pagi.style.display = &quot;none&quot;;
    siang.style.display = &quot;block&quot;;
}

else if(time &amp;gt;= 7 &amp;amp;&amp;amp; time&amp;lt;= 10){
    malam.style.display = &quot;none&quot;;
    pagi.style.display = &quot;block&quot;;
    siang.style.display = &quot;none&quot;;
}

else{
    malam.style.display = &quot;none&quot;;
    pagi.style.display = &quot;none&quot;;
    siang.style.display = &quot;none&quot;;
}

&lt;/script&gt;</pre><div><br><br></div>

avatar NauraFaradisa
@NauraFaradisa

12 Kontribusi 10 Poin

Dipost 3 tahun yang lalu

Login untuk ikut Jawaban