为什么初始CSS样式在DOM元素.style字段上不可见? [英] why are initial CSS styles not visible on DOM element .style field?

查看:110
本文介绍了为什么初始CSS样式在DOM元素.style字段上不可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我完全有理由问一些愚蠢的东西(至少是重复的东西),但是在所附的代码片段中,为什么我必须使用window.getComputedStyle来访问CSS所应用的样式?我的印象是.style字段至少会反映出CSS最初应用的那些样式,和/或此后手动更改的样式.

如果没有,控制在元素的.style字段中反映(以及何时反映)哪些属性的确切规则是什么?

 setTimeout(() => {
    console.log("the bckg color:", reddish.style.backgroundColor);
    console.log("the width:", reddish.style.width);
    console.log("from a computed style:", window.getComputedStyle(reddish).backgroundColor);
    console.log("from a computed style:", window.getComputedStyle(reddish).width);
}, 100); 

 #reddish {
    background-color: #fa5;
    width: 100px;
    height: 100px;
} 

 <html>
    <body>
        <div id="reddish"></div>
    </body>
</html> 

解决方案

HTMLElement.style 属性对于完全 了解应用于元素的样式,因为它表示 仅CSS声明设置为元素的内联样式 属性,而不是来自其他地方的样式规则的属性,例如 部分或外部样式表中的样式规则.要得到 您应该使用的元素的所有CSS属性的值 改为 Window.getComputedStyle().

通过- MDN Web文档|获得风格 信息


HTMLElement.style:

使用了 HTMLElement.style 属性 get 以及 set 元素的内联样式. >

 console.log(document.getElementById("para").style.fontSize); // will work since the "font-size" property is set inline
console.log(document.getElementById("para").style.color); // will not work since the "color" property is not set inline 

 #para {color: rgb(34, 34, 34);} 

 <p id="para" style="font-size: 20px;">Hello</p> 


Window.getComputedStyle():

getComputedStyle()方法,返回一个对象,其中包含元素的所有CSS属性的值,在应用了活动的样式表并解决了这些值可能包含的所有基本计算之后,因此从内联样式声明以及外部样式表中返回了css属性.

 console.log(window.getComputedStyle(document.getElementById("para")).fontSize); // will work
console.log(window.getComputedStyle(document.getElementById("para")).color); // will work 

 #para {
  color: rgb(34, 34, 34);
} 

 <p id="para" style="font-size: 20px;">Hello</p> 

OK I have full expectation of going down in flames for asking something stupid (or at least duplicate), but in the attached snippet, why do I have to use window.getComputedStyle to access styles applied by CSS? I was under the impression that the .style field would at least reflect those styles initially applied by CSS, and/or manually changed since then.

If not, what are the exact rules governing which properties are reflected (and when) in an element's .style field?

setTimeout(() => {
    console.log("the bckg color:", reddish.style.backgroundColor);
    console.log("the width:", reddish.style.width);
    console.log("from a computed style:", window.getComputedStyle(reddish).backgroundColor);
    console.log("from a computed style:", window.getComputedStyle(reddish).width);
}, 100);

#reddish {
    background-color: #fa5;
    width: 100px;
    height: 100px;
}

<html>
    <body>
        <div id="reddish"></div>
    </body>
</html>

解决方案

The HTMLElement.style property is not useful for completely learning about the styles applied on the element, since it represents only the CSS declarations set in the element's inline style attribute, not those that come from style rules elsewhere, such as style rules in the section, or external style sheets. To get the values of all CSS properties for an element you should use Window.getComputedStyle() instead.

Via- MDN Web Docs | Getting Style Information


HTMLElement.style:

The HTMLElement.style property is used to get as well as set the inline style of an element.

console.log(document.getElementById("para").style.fontSize); // will work since the "font-size" property is set inline
console.log(document.getElementById("para").style.color); // will not work since the "color" property is not set inline

#para {color: rgb(34, 34, 34);}

<p id="para" style="font-size: 20px;">Hello</p>


Window.getComputedStyle():

The getComputedStyle() method however, returns an object containing the values of all CSS properties of an element, after applying active stylesheets and resolving any basic computation those values may contain thus returning the css properties from both inline style declarations as well as from external style-sheets.

console.log(window.getComputedStyle(document.getElementById("para")).fontSize); // will work
console.log(window.getComputedStyle(document.getElementById("para")).color); // will work

#para {
  color: rgb(34, 34, 34);
}

<p id="para" style="font-size: 20px;">Hello</p>

这篇关于为什么初始CSS样式在DOM元素.style字段上不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆