Javascript 如何根据一周中的哪一天更改目标文本的字体颜色?

Javascript 如何根据一周中的哪一天更改目标文本的字体颜色?,javascript,html,Javascript,Html,我试图创建一个表行,其中每个单元格都有可视文本,这些文本会根据一周中的哪一天更改颜色。我用相应的日期和颜色将函数设置到每个单元格中。但我似乎无法让它正常工作。我不知道我是否遗漏了什么。请帮忙 <tr> <td><script> function(){ var theday = new Date().getDays(); if theday == 1 { document

我试图创建一个表行,其中每个单元格都有可视文本,这些文本会根据一周中的哪一天更改颜色。我用相应的日期和颜色将函数设置到每个单元格中。但我似乎无法让它正常工作。我不知道我是否遗漏了什么。请帮忙

<tr>
  <td><script>
    function(){
      var theday = new Date().getDays();                    
      if theday == 1 {
        document.write('S').style.color = #FF0000;
      }
      else:
        document.write('S').style.color = #FFFFFF;
      }}
  </td></script>                    
</tr>

函数(){
var theday=新日期().getDays();
如果日期=1{
document.write('S').style.color=#FF0000;
}
其他:
document.write('S').style.color=#FFFFFF;
}}
当前代码:

         <tr>
            <td><script>
                Date.prototype.getWeekDay = function(){
                    var theday = new Date().getDays();                    
                    if (theday == 1) {
                        document.write('S').style.color = #FF0000;
                    }
                    else
                        document.write('S').style.color = #FFFFFF;
                    }}
            </script></td>

Date.prototype.getWeekDay=函数(){
var theday=新日期().getDays();
如果(theday==1){
document.write('S').style.color=#FF0000;
}
其他的
document.write('S').style.color=#FFFFFF;
}}
我应该提到我正在使用Mozilla Firefox作为我的主要浏览器。 当前编辑:

        <tr>
            <td id='sun'></td>
            <td id='mon'></td>
            <td id='tue'></td>
            <td id='wed'></td>
            <td id='thr'></td>
            <td id='fri'></td>
            <td id='sat'></td>              
        </tr>

<script>
window.onload = function(){
   var theday = new Date().getDay();  
   var color = theday == 1 ? '#FF0000' : '#FFFFFF';
   var color = theday == 2 ? '#FF7F00' : '#FFFFFF';
   var color = theday == 3 ? '#FFFF00' : '#FFFFFF';
   var color = theday == 4 ? '#00FF00' : '#FFFFFF';
   var color = theday == 5 ? '#0000FF' : '#FFFFFF';
   var color = theday == 6 ? '#2E2B5F' : '#FFFFFF';
   var color = theday == 7 ? '#8B00FF' : '#FFFFFF';
   document.getElementById('sun').style.backgroundColor = color;
   document.getElementById('mon').style.backgroundColor = color;
   document.getElementById('tue').style.backgroundColor = color;
   document.getElementById('wed').style.backgroundColor = color;
   document.getElementById('thr').style.backgroundColor = color;
   document.getElementById('fri').style.backgroundColor = color;
   document.getElementById('sat').style.backgroundColor = color;
  }
</script>

window.onload=函数(){
var theday=新日期().getDay();
var color=theday==1?'#FF0000':'#FFFFFF';
var color=theday==2?'#FF7F00':'#FFFFFF';
var color=theday==3?“#FFFF00”:“#FFFFFF”;
var color=theday==4?'#00FF00':'#FFFFFF';
var color=theday==5?'#0000FF':'#FFFFFF';
var color=theday==6?“#2E2B5F”:“FFFFFF”;
var color=theday==7?“#8B00FF”:“#FFFFFF”;
document.getElementById('sun').style.backgroundColor=color;
document.getElementById('mon').style.backgroundColor=color;
document.getElementById('tue').style.backgroundColor=color;
document.getElementById('wed').style.backgroundColor=颜色;
document.getElementById('thr').style.backgroundColor=color;
document.getElementById('fri').style.backgroundColor=颜色;
document.getElementById('sat').style.backgroundColor=颜色;
}
控制台消息:

未捕获类型错误:(new Date())。getDays不是函数

更正了.getDays>.getDay中的代码输入错误

更新代码:

        <tr>
            <td id='sun'>S</td>
            <td id='mon'>M</td>
            <td id='tue'>Ts</td>
            <td id='wed'>W</td>
            <td id='thr'>Th</td>
            <td id='fri'>F</td>
            <td id='sat'>S</td>             
        </tr>

<script>
window.onload = function(){
   var theday = new Date().getDay();  
   var c1 = theday == 1 ? '#FF0000' : '#FFFFFF';
   var c2 = theday == 2 ? '#FF7F00' : '#FFFFFF';
   var c3 = theday == 3 ? '#FFFF00' : '#FFFFFF';
   var c4 = theday == 4 ? '#00FF00' : '#FFFFFF';
   var c5 = theday == 5 ? '#0000FF' : '#FFFFFF';
   var c6 = theday == 6 ? '#2E2B5F' : '#FFFFFF';
   var c7 = theday == 7 ? '#8B00FF' : '#FFFFFF';
   document.getElementById('sun').style.color = c1;
   document.getElementById('mon').style.color = c2;
   document.getElementById('tue').style.color = c3;
   document.getElementById('wed').style.color = c4;
   document.getElementById('thr').style.color = c5;
   document.getElementById('fri').style.color = c6;
   document.getElementById('sat').style.color = c7;
  }
</script>

s
M
Ts
W
Th
F
s
window.onload=函数(){
var theday=新日期().getDay();
var c1=日期==1?'#FF0000':'#FFFFFF';
变量c2=日期==2?'#FF7F00':'#FFFFFF';
变量c3=日期==3?'#FFFF00':'#FFFFFF';
变量c4=日期==4?'#00FF00':'#FFFFFF';
var c5=日期==5?'0000FF':'FFFFFF';
变量c6=日期==6?“#2E2B5F”:“FFFFFF”;
变量c7=日期==7?“#8B00FF”:“#FFFFFF”;
document.getElementById('sun').style.color=c1;
document.getElementById('mon').style.color=c2;
document.getElementById('tue').style.color=c3;
document.getElementById('wed').style.color=c4;
document.getElementById('thr').style.color=c5;
document.getElementById('fri').style.color=c6;
document.getElementById('sat').style.color=c7;
}
仍然没有像我想要的那样更新字体

最新更新:

        <tr>
            <td id='sun'>S</td>
            <td id='mon'>M</td>
            <td id='tue'>Ts</td>
            <td id='wed'>W</td>
            <td id='thr'>Th</td>
            <td id='fri'>F</td>
            <td id='sat'>S</td>             
        </tr>

<script>
window.onload = function(){
   var theday = new Date().getDay(); 
   var c1 = theday == 0 ? '#FF0000' : '#999999';
   var c2 = theday == 1 ? '#FF7F00' : '#999999';
   var c3 = theday == 2 ? '#FFFF00' : '#999999';
   var c4 = theday == 3 ? '#00FF00' : '#999999';
   var c5 = theday == 4 ? '#0000FF' : '#999999';
   var c6 = theday == 5 ? '#2E2B5F' : '#999999';
   var c7 = theday == 6 ? '#8B00FF' : '#999999';
   document.getElementById('sun').style.color = c1;
   document.getElementById('mon').style.color = c2;
   document.getElementById('tue').style.color = c3;
   document.getElementById('wed').style.color = c4;
   document.getElementById('thr').style.color = c5;
   document.getElementById('fri').style.color = c6;
   document.getElementById('sat').style.color = c7;
  }
</script>

s
M
Ts
W
Th
F
s
window.onload=函数(){
var theday=新日期().getDay();
var c1=日期==0?'#FF0000':'#999999';
变量c2=日期==1?'#FF7F00':'#999999';
变量c3=日期==2?'#FFFF00':'#999999';
变量c4=日期==3?'#00FF00':'#999999';
变量c5=日期==4?'0000FF':'999999';
var c6=日期==5?'#2E2B5F':'#999999';
变量c7=日期==6?“#8B00FF”:“#999999”;
document.getElementById('sun').style.color=c1;
document.getElementById('mon').style.color=c2;
document.getElementById('tue').style.color=c3;
document.getElementById('wed').style.color=c4;
document.getElementById('thr').style.color=c5;
document.getElementById('fri').style.color=c6;
document.getElementById('sat').style.color=c7;
}
这成功了!谢谢大家!


星期日
星期一
星期二
星期三
星期四
星期五
星期六
var theday=新日期().getDay();
变量颜色={
‘sun’:‘206AE6’,//星期日
“mon”:“cccc”,//moday
‘星期二’:‘999999’,//星期二
“星期三”:“666666”//星期三
‘thu’:‘333333’,//周四
‘fri’:‘000000’,//fri
“星期六”:“ff0000”,//星期六
};
window.onload=函数(){
var headerrow=document.querySelectorAll(“#calendarTable tr.header td”);
人头费(el=>{
el.style.color=颜色[el.id];
})
}

将其更改为
if(theday==1)
else
。控制台中一定有错误。在进行这些更改后,这是我从控制台得到的错误:未捕获的语法错误:当前不支持私有字段这是我通过编辑得到的错误消息:未捕获的类型错误:(new Date())。getDays不是一个函数抱歉@JustinBache-一个打字错误
new Date().getDay()
我在上面更正了它,并将它放在一个可以测试的代码段中。我在firefox上不断发现编码错误。字体默认为第二颜色。我肯定认为我离我想要的答案越来越近了though@JustinBache-我更新了代码以反映。把你的颜色放在一个数组中,然后用
日期引用它们,日期总是一个数字,0代表星期天,1代表星期一。。。等等。@JustinBache-注意这个新方法。在obj中首先指定所有颜色