[英]Why can't I access the style element in Javascript? Firefox console error message says it's “Null”
I use scss and my stylesheet is a minified sheet syle.min.css.我使用 scss,我的样式表是一个缩小的表 syle.min.css。 I'm trying to make the analog clock project and the style element would get the rotation variable and rotate the hands of the clock respectively.
我正在尝试制作模拟时钟项目,样式元素将分别获得旋转变量并旋转时钟的指针。
The code:编码:
setInterval(setClock, 1000);
const hourHand = document.querySelector('[data-hour-hand]');
const minuteHand = document.querySelector('[data-minute-hand]');
const secondHand = document.querySelector('[data-second-hand]');
//get the current date
function setClock() {
const currentDate = new Date();
//gets the current seconds
const secondsRatio = currentDate.getSeconds() / 60;
//gets the current minutes plus the socnds so it can gradually glide around the clock
const minutesRatio = (secondsRatio + currentDate.getMinutes()) / 60;
//gets the current hours plus the socnds so it can gradually glide around the clock
const hoursRatio = (minutesRatio + currentDate.getHours()) / 12;
setRotation(secondHand, secondsRatio);
setRotation(minuteHand, minutesRatio);
setRotation(hourHand, hoursRatio);
}
//rotates the hands clockwise
function setRotation(element, rotationRatio) {
//this is gonna take what property we want to set this in css so the ratation variable times the 360
element.style.setProperty('--rotation',rotationRatio * 360);
}
The problem was the structure.问题是结构。 As Martin said, I moved my script tag down and it stops it from returning that error message.
正如马丁所说,我将我的脚本标签向下移动,它阻止它返回该错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.