如何使用javascript加粗日期和时间

如何使用javascript加粗日期和时间,javascript,Javascript,我试图在我的网站上发布时间,我希望根据javascript自动将日期和时间加粗。是否可以设置一些javascript,以便在周一使用粗体,然后在周二使用粗体等等 代码如下: <div id="monday">Monday: 12:00-2:00</div> <div id="tuesday">Tuesday: 11:00-3:00</div> 周一:12:00-2:00 星期二:11:00-3:00 每天如此。当用户在星期一访问站点时,我希望星

我试图在我的网站上发布时间,我希望根据javascript自动将日期和时间加粗。是否可以设置一些javascript,以便在周一使用粗体,然后在周二使用粗体等等

代码如下:

<div id="monday">Monday: 12:00-2:00</div>
<div id="tuesday">Tuesday: 11:00-3:00</div>
周一:12:00-2:00
星期二:11:00-3:00
每天如此。当用户在星期一访问站点时,我希望星期一div将其中的所有内容都加粗。当用户在星期二访问站点时,我希望整个星期二div都用粗体显示


谢谢

如果您希望日期和时间加粗,只需使用document.write在javascript中围绕日期/时间(甚至整个日期时间)喷射相关的or标记,或者使用jQuery操作javascript元素,例如day?

如果您希望日期和时间加粗,只需围绕日期/时间喷射相关的or标记即可(甚至是整个日期时间)在javascript中使用document.write,或者使用jQuery操作javascript元素,例如Daily?

如果您可以在时间文本周围放置任何标记,那么就没有理由让JS参与格式化-同样,您可以像放置标记一样从脚本格式化,但如果不能这样做,则是唯一的解决方案(有这么多细节)正在扫描整个页面或选定的元素,查找datatime模式,重新格式化并返回页面(正则表达式将很有帮助)

好的,我们有一些细节

var x = document.getElementByID('monday').innerHTML; document.getElementByID('monday').innerHTML = '<B>' + x + '</B>'; var x=document.getElementByID('monday').innerHTML; document.getElementByID('monday').innerHTML=''+x+'';
如果您可以在时间文本周围放置任何标记,那么就没有理由让JS参与格式化-您可以像放置标记一样从脚本中进行格式化,但是如果无法做到这一点,唯一的解决方案(有这么多详细信息)是扫描整个页面或选定的元素,查找datatime模式,重新格式化并返回页面(正则表达式会有帮助)

好的,我们有一些细节

var x = document.getElementByID('monday').innerHTML; document.getElementByID('monday').innerHTML = '<B>' + x + '</B>'; var x=document.getElementByID('monday').innerHTML; document.getElementByID('monday').innerHTML=''+x+''; 像这样:

示例:


编辑:

下面是正在发生的事情的分解。我将使其更加详细,将代码扩展为不同的变量,以便更容易查看

   // 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";
请注意,不需要将一串天转换为数组。只需稍微缩短键入时间并加快键入速度即可

你可以做:

var days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];
这将创建一个数组,因此您不需要像这样执行
.split()

示例:


编辑:

下面是正在发生的事情的分解。我将使其更加详细,将代码扩展为不同的变量,以便更容易查看

   // 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";
请注意,不需要将一串天转换为数组。只需稍微缩短键入时间并加快键入速度即可

你可以做:

var days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];

这将创建一个数组,因此您无需执行
.split()

是否需要包含更多详细信息?哪个页面?在页面上发布您的代码您需要提供更多详细信息。您需要“星期一”、“星期二”这些词吗,等等,以粗体显示,文本字符串?对不起,我添加了更多的详细信息以包含更多的详细信息?哪一页?发布你的页面代码你必须提供更多的详细信息。你想要“星期一”、“星期二”这些词吗,等等,以粗体显示,在一个字符串中?对不起,我添加了更多的细节如果你不介意我问的话,为什么它只在代码放在结束标记附近时才起作用?当我在头部或正文开头尝试它时,它不起作用。另外,你介意解释一下这一切的含义吗:(days[(new Date()).getDay()).类名。Thanks@golf_nut:我将其放在底部的原因是
getElementById()
正在选择元素。元素必须存在才能被选择,因此如果将代码放在工作日元素之前,将找不到任何东西。还有其他方法可以实现这一点,但将其放在底部是一个很好的方法。我将在回答中添加对代码的进一步解释。如果您不介意的话我在问,为什么只有当代码放在结束标记附近时它才起作用?当我在头部或身体的开头尝试它时,它不起作用。另外,你介意解释一下这一切意味着什么吗:(days[(new Date()).getDay()).类名。Thanks@golf_nut:我将其放在底部的原因是
getElementById()
正在选择元素。元素必须存在才能被选择,因此如果将代码放在工作日元素之前,将找不到任何东西。也有其他方法可以实现这一点,但将其放在底部是一种非常好的方法。我将在回答中添加对代码的进一步解释。