如何添加一个类,而没有它摆脱其他人? [英] How do I add a class without having it get rid of the others?

查看:127
本文介绍了如何添加一个类,而没有它摆脱其他人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页脚中设置了以下代码,在指定的时间内添加了一个'day'类。

I have the following code set up in my footer which adds a class of 'day' during the time specified.

function setTimeStyles() {
    var currentTime = new Date().getHours();
    if(currentTime > 5 && currentTime < 17) {
    document.body.className = 'day';
    }
}

$(document).ready(function() {
    setTimeStyles();
});

问题是当类被添加时,它摆脱了其他已经存在的类。例如,如果没有添加'day'类,我的body标签将是:

The problem is when the class gets added, it gets rid of the other classes already in place. For example, without the 'day' class added, my body tag would be:

<body class="home">

但是当添加'day'类时,我的body标签变成:

But when the 'day' class is added, my body tag turns into:

<body class="day">

我想这样,'day'类被添加到已经存在的东西像这样:

I'd like it so that the 'day' class gets added to what's already there like this:

<body class="home day">

我该如何做?

推荐答案

在jQuery中,您可以执行

In jQuery you can do

$("body").addClass("day");

这篇关于如何添加一个类,而没有它摆脱其他人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆