实时时钟不到15分钟时改变元素的样式?

时间:2017-02-07 12:38:34

标签: javascript jquery css

我希望在实时时钟上的分钟数小于15时更改div的背景。这是我的代码:

<script type="text/javascript">
window.setInterval(function(){
  now = new Date;
c = now.getMinutes();
if (c < 15 ) {
$('#defaultCountdown').css('background': 'red');
}
}, 1000);

</script>

这里是页面的链接,它不会变红,c&lt;设置50:

http://signalsindicator.com/timer/index.html

1 个答案:

答案 0 :(得分:5)

更改

$('#defaultCountdown').css('background': 'red');

$('#defaultCountdown').css('background', 'red');

或将其更改为

$('#defaultCountdown').css({
  'background': 'red'
});

&#13;
&#13;
window.setInterval(function(){
  now = new Date;
c = now.getMinutes();
if (c < 15 ) {
$('#defaultCountdown').css('background', 'red');
}
else{
 $('#defaultCountdown').css('background', 'blue'); 
}
}, 1000);
&#13;
div{
  height:200px;
  width:200px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="defaultCountdown"></div>
&#13;
&#13;
&#13;