如何用javascript加大日期和时间

gol*_*nut 0 javascript

我正试图在我的网站上发布小时数,我想要基于javascript自动加粗的日期和时间.是否有可能设置一些javascript,以便周一将是粗体,然后在星期二将是大胆的等等?

这是代码:

<div id="monday">Monday: 12:00-2:00</div>
<div id="tuesday">Tuesday: 11:00-3:00</div>
Run Code Online (Sandbox Code Playgroud)

等等每一天.当用户在星期一访问该站点时,我希望星期一div加粗那里的所有内容.当用户在星期二访问该站点时,我希望整个星期二div加粗.

谢谢

use*_*716 5

像这样:

示例: http ://jsfiddle.net/c5bHx/

<!DOCTYPE html>
<html>
    <head><title>title</title></head>
    <body>

        <!-- your content -->

             <!-- place this just inside your closing </body> tag -->
        <script type="text/javascript>
            var days = 'sunday,monday,tuesday,wednesday,thursday,friday,saturday'.split(',');
            document.getElementById( days[(new Date()).getDay()] ).className = 'bold';
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

CSS

.bold {
    font-weight:bold;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

这是对正在发生的事情的细分.我将使它更冗长,将代码扩展为不同的变量,因此更容易看到.

   // This simply creates a string of days in the week
var days = 'sunday,monday,tuesday,wednesday,thursday,friday,saturday';

   // This splits the string on the commas, turning it into an Array of weekdays
days = days.split(',');

   // Create a new Date object, representing today's date and time
var date = new Date();

   // Get the number of the day of the week. 0 if sunday, 1 if monday, etc...
var dayNumber = date.getDay();

   // Using the "dayNumber", get the day string by its index from the "days" array
var dayString = days[ dayNumber ];

   // Select the element on the page that has the ID that matches the "dayString"
var dayElement = document.getElementById( dayString );

   // Set the "class" property of the "dayElement" to the "bold" class.
dayElement.className = "bold";
Run Code Online (Sandbox Code Playgroud)

请注意,不需要将天数字符串转换为数组.只需更短一点,更快捷的打字.

你可以这样做:

var days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];
Run Code Online (Sandbox Code Playgroud)

这会创建一个数组,所以你不需要这样做.split().