Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

jquery - addClass to body won't work

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%);}`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...