简体   繁体   English

无法访问元素样式属性

[英]can't access element style properties

I want to change some styles of my 'text' variable but somehow can't.我想更改我的“文本”变量的一些 styles 但不知何故不能。

const bg = document.querySelector(".bg");
const text = document.querySelector('.loading-text');


let count = 0;

let interval = setInterval(blurr,30);

 function blurr() {
    if(count>99) {
        clearInterval(interval);
    }
    text.innerHTML = `${count}%`;
    text.style //here is the problem
    count++;
};

Im new here, and new to javascript so go easy on me please:|我是新来的,也是 javascript 的新手,所以 go 请放轻松:|

What are you trying to do regarding the element styling?关于元素样式,你想做什么? Are you trying to get the current value or set it?您是要获取当前值还是要设置它?

In order to get some CSS styling property you can do as follows:为了获得一些 CSS 样式属性,您可以执行以下操作:

var color = text.style.color;

If you are trying to set/change it, you can do as follows:如果您尝试设置/更改它,您可以执行以下操作:

test.style.color = 'green'; 

See this documentation for more information: https://www.w3schools.com/js/js_htmldom_css.asp有关详细信息,请参阅此文档: https://www.w3schools.com/js/js_htmldom_css.asp

Edit: From what I can tell OP is trying to set the opacity on the element.编辑:据我所知,OP 正在尝试设置元素的不透明度。

text.style.opacity = 0.01 * count;

See this working here: https://jsfiddle.net/84pohswj/在这里看到这个工作: https://jsfiddle.net/84pohswj/

But if that is the case I would suggest you use CSS animations instead.但如果是这种情况,我建议您改用 CSS 动画。 There much better option for this.有更好的选择。 And use JS to assign the animation class: https://www.w3schools.com/css/css3_animations.asp并用JS给animation class赋值: https://www.w3schools.com/css/css3_animations.asp

if you want to change your opacity to 0.5 here is the code如果你想改变你的不透明度为 0.5 这里是代码

text.style.opacity = "0.5";

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM