简体   繁体   English

为什么我无法访问 Javascript 中的样式元素? Firefox 控制台错误消息说它是“空”

[英]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.

相关问题 如何修复 Firefox 控制台中的“不是函数”JavaScript 错误? - How can I fix a “not a function” JavaScript error in Firefox's console? 控制台说元素为“ null” - Console says element is “null” 为什么我不能使用JavaScript在Chrome中直接分配给element.style? - Why can't I directly assign to element.style in Chrome using JavaScript? (不能)在 javascript 中获取元素样式的一部分 - (can't) Get part of an element's style in javascript 如何捕获 Firefox 的套接字连接错误消息? - How can I capture Firefox's socket connection error message? 为什么我无法访问 Javascript 中新添加的元素(模式弹出窗口),即使在开发者控制台中也是如此? - Why can't I access newly added elements (modal popup) in Javascript, even in the developer console? Firefox JavaScript 错误“未定义”(div Id)。控制台中的样式 - Firefox JavaScript error “undefined” (div Id).style in console jQuery - console.log()返回一个元素,但我无法访问它 - jQuery - console.log() returns an element but I can't access it JavaScript控制台可以看到对象的内容,但是我无法访问它 - JavaScript console can see contents of Object but I can't access it JavaScript无法运行,表示element为null,但是代码运行得很混乱 - JavaScript won't run, says element is null, but code runs in fiddle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM