首页 > 解决方案 > Javascript在特定时间更改文本阴影方向

问题描述

我正在尝试使用 javascript 在晚上 9 点到 10 点之间更改文本阴影的方向。

尝试了许多更改,但不确定我哪里出错了,这是我的代码的基础知识..

<head>

<style>

 body {
    text-shadow: 12px 12px 8px #000000;
}

</style>

<script>

var currentTime = new Date().getHours();

if (21 <= currentTime && currentTime < 22) 
{
document.getElementById('body').text.style.textShadow  = "-12px 12px 8px 
#000000";
}

</script>

</head>

.. 没有包含正文,但我的文本在 <div class="..." > 和 <a href="..." > 中,如果这有帮助?

所以通常文本阴影应该在右边,晚上 9 点到 10 点应该在左边?

谢谢!

标签: javascriptcsstimeshadow

解决方案


根据评论,我认为这是错误的:

document.getElementById('body').text 

一个示例片段来澄清:

document.addEventListener('DOMContentLoaded', function(e) {
  document.querySelector('button').addEventListener('click', function(e) {
      document.querySelectorAll('div, a').forEach(function(ele) {
         ele.style.textShadow  = "-12px 12px 8px #000000";
      });
  })

});
body {
    text-shadow: 12px 12px 8px #000000;
}
<div>
    this is a text iside a div......
</div>
<a href="">this is a text inside an anchor......</a>
<br/>
<br/>
<button type="button">Click Me</button>


推荐阅读