Javascript 显示一周中的天数
我想在列表中显示一周中的几天。对于当前日期,我希望显示单词“今天”。如何使用javascript实现这一点Javascript 显示一周中的天数,javascript,jquery,Javascript,Jquery,我想在列表中显示一周中的几天。对于当前日期,我希望显示单词“今天”。如何使用javascript实现这一点 <ul class="week-days"> <li><a href="#"><span id="firstD">Today</span></a></li> <li><a href="#"><span id="secondD">Tue</span&g
<ul class="week-days">
<li><a href="#"><span id="firstD">Today</span></a></li>
<li><a href="#"><span id="secondD">Tue</span></a></li>
<li><a href="#"><span id="thirdD">Wed</span></a></li>
<li><a href="#"><span id="forthD">Thu</span></a></li>
<li><a href="#"><span id="fifthD">Fri</span></a></li>
<li><a href="#"><span id="sixthD">Sat</span></a></li>
<li><a href="#"><span id="seventhD">Sun</span></a></li>
</ul>
这里有一种方法:
// Find the unordered list element
var list = $('ul.week-days');
// Check what day it is today (0..6)
var today = (new Date()).getDay();
// Our day strings; in JS, "Sunday" is the beginning of the week
var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
// Add a list item to the list, substituting "Today" if necessary
$.each(days, function(i, v) {
list.append($('<li>' + (i == today ? 'Today' : v) + '</li>'));
});
//查找无序列表元素
风险值列表=$('ul.周天数');
//检查今天是星期几(0..6)
var today=(新日期()).getDay();
//我们的日子过得很好;在JS中,“星期天”是一周的开始
var days=[‘星期日’、‘星期一’、‘星期二’、‘星期三’、‘星期四’、‘星期五’、‘星期六’];
//在列表中添加列表项,必要时替换为“今天”
美元/天,功能(i,v){
追加($(“”+(i==今天?”:v)+“ ”);
});
当然,它缺少
,您可以通过在页面底部(就在body-closing标记的上方)添加该标记来实现这一点
var i=1;
$('.week days li')。每个(函数(){
如果((新日期()).getDay()==i){
$(this.val('Today').html('Today');
}
i++;
});
您可以这样做(只有在遵守3个字母缩写的情况下)
我不明白你想解决的问题。您发布了一个关于JavaScript的问题,但您发布的唯一代码是标记。你能澄清你的问题吗?见谢谢@Arun。你的答案是最好的,效果很好。看起来有个bug。。。。因为运算符优先级。。。看见
<script type="text/javascript">
var i = 1;
$('.week-days li').each(function(){
if ((new Date()).getDay() == i) {
$(this).val('Today').html('Today');
}
i++;
});
</script>
var date = new Date().toString(); // Get the Date and return it as a string
var day = date.split(' ')[0]; // Split the date and store the first index (day)
$('.week-days li').each(function(){ // loop over the li
if($(this).html() === day){ // if this html is the same as day variable
$(this).html('Today'); // change the html to 'Today'
}
});