English 中文(简体)
根据一天中的时间更改身体和链接的类别
原标题:Change class of body and links depending on time of day

我试图根据一天中的时间更改代码中的几个类。我可以很好地改变背景,但我也需要改变菜单的颜色。我似乎无法在页面中的链接中添加一个类来让它们也发生变化!不知道我哪里做错了,任何帮助都将不胜感激,谢谢!

附言:我也试过jQuery cs(),也没有成功。。。

function getDayTime(hours) {
if (hours > 20 || hours < 5)
    return "night";

if (hours > 17)
    return "dusk";

if (hours > 8)
    return "day";

return "dawn";
}

$(function() {
    document.body.className = getDayTime(new Date().getHours());
});

太棒了,非常感谢大家的帮助,我更改了javascript,它也很好用。然而,我现在也在尝试将黎明的颜色改为白色——即使我将其编码为白色,它仍然保持为红色!显然,我的身体里没有足够的咖啡。。。

body.dawn #menu a {
    color:#fff !important
}

body.day #menu a {
    color:#8a0000 !important
}

body.dusk #menu a {
    color:#fff !important
}

body.night #menu a {
    color:#fff !important
}
问题回答

在jQuery中,你没有类名。查看addClass和类似的。

然而,我建议你在CSS中加入这样的内容:

body.night a {
    /* your style for the  nightlink  with !important to be sure */
}

因此,您不需要要求JS检索和迭代页面中的所有A节点。此外,如果你要动态添加链接,使用CSS方法,样式也会自动应用于它们。

编辑:与原始问题无关,但我认为你可以用这样的方式简化js代码中的条件:

function getDayTime(hours) {
    if (hours > 20 || hours < 5)
        return "night";

    if (hours > 17)
        return "dusk";

    if (hours > 8)
        return "day";

    return "dawn";
}

$(function() {
    document.body.className = getDayTime(new Date().getHours());
});

我不知道你的CSS是什么样子的,但你可以这样做:

假设你的CSS看起来像这样:

<style type="text/css">
  body { /* some stuff here */ }
  body.day { /* some day stuff here */ }
  body.night { /* some night stuff here */ }
  #menu { /* some stuff here */ }
  #menu a { /* some menu link styling here */ }

  /* To change #menu when body has class="day" */
  body.day #menu { // change menu when it s day }
  body.day #menu a { // change menu links */ }

  /* To change #menu when body has class"night" */
  body.night #menu { /* change menu when it s night */ }
  body.night #menu a { // change menu links */ }
</style>
$("a").addClass("nightlink")

应该工作





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签