问题
I'm trying to make my background change during the day depending on the time, tried 4 different methods and searched here, but still can't figure it out. Can anybody tell me what i'm doing wrong? Thanks in advance!
$(document).ready(function(){
var d = new Date();
var n = d.getHours();
if (n > 6 || n < 10)
$(body).addClass('two');
else if (n > 10 && n < 17)
$(body).addClass('one');
else if (n > 17 && n <= 19)
$(body).addClass('three');
else
$(body).addClass('five');
});
CSS
.one{
background-image: linear-gradient(-179deg, #8EB39E 0%, #CEBD95 26%, #DBBF94 33%, #D48B7A 67%, #8B374E 100%);}
.two{
background-image: linear-gradient(-180deg, #FFEC96 0%, #92C684 65%, #185C34 100%);}
.three{
background-image: linear-gradient(-180deg, #FFEC96 0%, #92C684 65%, #185C34 100%);}
.five{
background-image: linear-gradient(0deg, #FAF2D8 0%, #F0CCB7 20%, #7F9993 46%, #1E6D74 69%, #0B3134 93%);}`
回答1:
If you meant the html body
tag, then wrap body
in quotes like you would with any CSS Selector
. In your case $("body").addClass("two");
, otherwise jQuery
will treat it as a variable.
来源:https://stackoverflow.com/questions/35365644/addclass-to-body-wont-work