简体   繁体   English

使用 window.getComputedStyle 错误的 css 信息

[英]Wrong css informations by using window.getComputedStyle

I have this html code我有这个 html 代码

<div id="testdiv3" style="border-left: 2px solid green; border-top: 4px dashed red; border-bottom: 3px dotted blue;">testcontent</div>

For the testdiv above, I see these styles in the console/styles对于上面的testdiv,我在console/styles中看到了这些样式

Element {
    border-left: 2px solid green;
    border-top: 4px dashed red;
    border-bottom: 3px dotted blue;
}

so it is, and so it should be noticed in the data which I have to use in jquery.确实如此,因此应该在我必须在 jquery 中使用的数据中注意到这一点。

in my jscode, I catch the css-styles by using在我的 jscode 中,我使用

...window.getComputedStyle...

so far so good.到目前为止,一切都很好。

Now in my testcase I see a situation I don't understand and therefore I have to create a way to prevent it.现在在我的测试用例中,我看到了一个我不理解的情况,因此我必须创建一种方法来防止它。

In contrast to the shown css-styles in the console I received this in jquery/jscode与控制台中显示的 css 样式相反,我在 jquery/jscode 中收到了这个

"Object { style_name: "border-bottom-color", style_value: "rgb(0, 0, 255)", style_unit: null }
​"Object { style_name: "border-left-color", style_value: "rgb(0, 128, 0)", style_unit: null }
"Object { style_name: "border-right-color", style_value: "rgb(71, 76, 80)", style_unit: null }
"Object { style_name: "border-top-color", style_value: "rgb(255, 0, 0)", style_unit: null }​

even though the "bottom-top-right" is not set to a border color, I received a border color.即使“右下角”未设置为边框颜色,我也收到了边框颜色。 Why?为什么?

I think the color is from the body, in his css is a color defined "color #474c50" what matched the "rgb(71, 76, 80)"...我认为颜色来自身体,在他的 css 中是一种颜色定义为“颜色 #474c50”,与“rgb(71, 76, 80)”相匹配......

So how can I either prevent that there will return a css-style which is not for this css-kind (which is not really in use) or how can I check if this style is an "own" style or a kind of inherited style?那么我怎样才能防止返回一个不适合这个 css 类型(实际上没有使用)的 css 样式,或者我如何检查这种样式是“自己的”样式还是一种继承的样式?

Additional to this, an inherited style, will use active for the style the page.除此之外,继承的样式将用于页面样式的活动。 This style is not in use, so I cannot understand why it will be returned.这个样式没有被使用,所以我不明白为什么它会被退回。

Hope somebody could help me with this (and I hope I could explain it to understand what's my situation and needs).希望有人可以帮助我(我希望我可以解释它以了解我的情况和需求)。

thanks a lot.多谢。

Please check the console of http://jsfiddle.net/ydrsx4jc/2/ It should explain a lot.请检查http://jsfiddle.net/ydrsx4jc/2/的控制台它应该解释很多。

To bring it on the point: Each DOM element has default attributes.重点是:每个 DOM 元素都有默认属性。 These attributes are controlable with css.这些属性可以用 css 控制。

Even if you set the border attribute for one side to none html will set the border to it's "default" value.即使您将一侧的border属性设置为none html 也会将border设置为其“默认”值。

You could say: If you are using only three sides of the border you are using the fourth, too.你可以说:如果你只使用border三个边,你也在使用第四个边。 Even if you don't want to and even if you don't see it.即使你不想,即使你没有看到它。

The computed value is indeed this value because all CSS rules have default values [citation needed] , set by the browser itself if you don't set it (aka user agent rules ) and that the one of border-[]-color rules is currentColor which when serialized must return the serialization of element's active color rule.计算出的值确实是这个值,因为所有 CSS 规则都有默认值[citation needed] ,如果您不设置它,则由浏览器本身设置(又名用户代理规则),并且border-[]-color规则之一是currentColor序列化时必须返回元素的活动color规则的序列化。

It can indeed be the one of the body if your element only inherits its color from there.如果您的元素仅从那里继承其color ,它确实可以成为主体之一。

But if you set its color to some other value, then this value will be used.但是,如果您将其color设置为其他值,则将使用该值。

And to your case, maybe you will rather want to check the width and style values instead.对于您的情况,也许您更愿意检查widthstyle值。

 const computed = getComputedStyle(el); console.log(computed['border-right-color']); // rgb(255, 165, 0) console.log(computed['border-right-style']); // none console.log(computed['border-right-width']); // 0px
 #el { border-left: 2px solid green; border-top: 4px dashed red; border-bottom: 3px dotted blue; /* defaults: */ /* border-right: 0px none currentColor; */ color: rgb(255, 165, 0); }
 <div id="el"></div>

Note getComputedStyle(el) return value in px注意 getComputedStyle(el) 返回值以 px 为单位

if you want to change other measurement like pt,mm,in,%,em如果您想更改其他测量值,例如 pt,mm,in,%,em

Convert Your value like转换你的价值

pt = 3/4*px pt = 3/4*px

em = pt/12 em = pt/12

% = pt*100/12 % = pt*100/12

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

相关问题 将window.getComputedStyle用于带有ps calc的pseuso元素样式 - Using window.getComputedStyle for pseuso elements style with css calc window.getComputedStyle 总是以像素为单位返回测量值 - window.getComputedStyle always returns measurement in pixels 如何修复Window.getComputedStyle不是对象错误 - How to fix Window.getComputedStyle is not an object error Window.getComputedStyle不显示内联样式 - Window.getComputedStyle does not show inline style 我想在javascript中使用window.getComputedStyle()来获取font-size,但它不能 - I want to get the font-size using window.getComputedStyle() in javascript but it can't 无法使用 window.getComputedStyle 或 element.offsetHeight 强制 DOM 重绘? - Cannot force DOM redraw using window.getComputedStyle or element.offsetHeight? 如何从window.getComputedStyle()或其他函数返回rgba值? - How to return rgba value from window.getComputedStyle() or other function? Window.getComputedStyle的参数1未实现接口Element - Argument 1 of Window.getComputedStyle does not implement interface Element window.getComputedStyle不适用于除Chrome之外的其他浏览器的速记属性 - window.getComputedStyle not working for shorthand properties in other browsers except Chrome 如何使用 window.getComputedStyle() 获得全高(包括边距)? - How to get full height (including margins) with window.getComputedStyle()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM