不断收到“无法读取属性错误”

时间:2019-07-12 20:03:54

标签: javascript

继续收到错误消息“未捕获的TypeError:无法在setDate读取null的'style'属性”不确定是否缺少即时消息!

 <script>
        const secondHand = document.querySelector('.second-Hand');
        const minsHand = document.querySelector('.min-Hand');
        const hourHand = document.querySelector('.hour-Hand');

        function setDate(){
          const now = new Date();

          const seconds = now.getSeconds();
          const secondsDegrees = ((seconds / 60) * 360) + 90;
          secondHand.style.transform = rotate(`${secondsDegrees}deg`);
          console.log(seconds);

          const mins = now.getMinutes();
          const minsDegrees = ((seconds / 60) * 360) + 90;
          hourHand.style.transform = `rotate(${minsDegrees}deg)`
        }

        setInterval(setDate, 1000);

</script>

1 个答案:

答案 0 :(得分:2)

您的querySelectors可能是错误的。二手也许应该是。二手,而您的分钟数也是错误的,反引号也应如下所示:

secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const secondHand = document.querySelector('.second-hand');
const minsHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');

function setDate() {
  const now = new Date();

  const seconds = now.getSeconds();
  const secondsDegrees = ((seconds / 60) * 360) + 90;
  secondHand.style.transform = `rotate(${secondsDegrees}deg)`;

  const mins = now.getMinutes();
  const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
  minsHand.style.transform = `rotate(${minsDegrees}deg)`;
}

setInterval(setDate, 1000);