addClass to body won't work

試著忘記壹切 提交于 2019-12-13 23:27:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!