admin管理员组

文章数量:1330597

I've only had a very quick preliminary search but is it possible to automatically change a specific element with javascript or jquery depending on the time?.. or by the local time of the puter who is viewing the webpage.

For instance say I have a <h2>Good Morning</h2> display before 12pm, and then automatically it would change to to <h2>Good Afternoon</h2> once it gets past this time.

I honestly, have not looked very deep into at all, but just wondering if this possible, and if so, is there a particular plugin that would aide me with this/what is the best way to do it?

I've only had a very quick preliminary search but is it possible to automatically change a specific element with javascript or jquery depending on the time?.. or by the local time of the puter who is viewing the webpage.

For instance say I have a <h2>Good Morning</h2> display before 12pm, and then automatically it would change to to <h2>Good Afternoon</h2> once it gets past this time.

I honestly, have not looked very deep into at all, but just wondering if this possible, and if so, is there a particular plugin that would aide me with this/what is the best way to do it?

Share Improve this question asked Nov 12, 2014 at 5:37 DanDan 131 silver badge3 bronze badges 2
  • 1 .getHours() and .text() – Regent Commented Nov 12, 2014 at 5:40
  • 1 Yes it is possible and you don't need any plugins. You just need to check the docs on the Date object in javascript. – Vivek Pradhan Commented Nov 12, 2014 at 5:40
Add a ment  | 

3 Answers 3

Reset to default 7

Don't need jQuery.

document.getElementById('greeting').innerHTML = new Date().getHours() >= 12 ? 'Good Afternoon' : 'Good Morning';

Javascript's Date object is you best bet to get the time.

http://www.w3schools./jsref/jsref_obj_date.asp

Once you know the time, it's easy to change the text in the header.

The text changes, might be done using cron job , if it is possible , you can do it just add or activate the css for the same element.

本文标签: javascriptChanging text depending on current timeStack Overflow