开发者

Select current date on calendar with javascript and html

开发者 https://www.devze.com 2023-03-19 08:53 出处:网络
I created a simple calendar with html and n开发者_如何学Goow want the current date to highlight automatically with javascript.I know of a few ways to do this but I am looking for the most simple.It re

I created a simple calendar with html and n开发者_如何学Goow want the current date to highlight automatically with javascript. I know of a few ways to do this but I am looking for the most simple.


It really depends on how your calendar works. You can get the client's current date with the following JavaScript:

var currentDate = new Date();

Once you have that, you'll have to use the built in date functions to get the current date element and probably add a class to it that will style it as highlighted.

UPDATE

Assuming you have your li elements with id="dayElement_x" where x is the day number, and your class for highlighting your day is currentDay, an example JavaScript call would be:

document.getElementById('dayElement_' + (new Date()).getDate()).className += ' currentDay';

UPDATE 2

I just thought of a solution where you can do this without having a bunch of ids. Here is the JavaScript:

document.getElementById('calendarContainer').
    getElementsByTagName('li')[(new Date()).getDate() - 1].className += ' currentDate';
0

精彩评论

暂无评论...
验证码 换一张
取 消