简体   繁体   English

javascript style.textDecoration 包含意外的 rgb 值?

[英]javascript style.textDecoration contains an unexpected rgb value?

Why does style.textDecoration contain an rgb value?为什么 style.textDecoration 包含 rgb 值?

 style = window.getComputedStyle(document.getElementById('gettextDecorationfrom')); weight = style.textDecoration; document.getElementById("results").innerText = style.textDecoration;
 .myclass { text-decoration:initial; }
 <div id="gettextDecorationfrom" class="myclass"> Test </div> <hr> Result: <div id="results"></div>

I was expecting "initial" to be the result from this but was not expecting an rgb value to show up.我期待“初始”是由此产生的结果,但没想到会出现 rgb 值。

If underline is set the rgb value is also still there but just with 'underline' added, I don't understand why rgb is there in the first place?如果设置了下划线,rgb 值也仍然存在,但只是添加了“下划线”,我不明白为什么 rgb 首先存在?

 style = window.getComputedStyle(document.getElementById('gettextDecorationfrom')); weight = style.textDecoration; document.getElementById("results").innerText = style.textDecoration;
 .myclass { text-decoration:underline; }
 <div id="gettextDecorationfrom" class="myclass"> Test </div> <hr> Result: <div id="results"></div>
I was expecting "underline" to be the result from this but was not expecting an rgb value as well to show up. 我期待“下划线”是由此产生的结果,但没想到 rgb 值也会出现。

Can I remove the rgb value from text-decoration no matter what the value is?我可以从 text-decoration 中删除 rgb 值,不管值是多少吗?

text-decoration has a default color, usually your browser's (black) text-decoration有默认颜色,通常是浏览器的颜色(黑色)

You can trim of the rgb part of the string with .replace and a bit of regex.您可以使用.replace和一些正则表达式修剪字符串的 rgb 部分。

This will cut out the rgb part regardless of the rgb(xxx) values, as you wanted.无论 rgb(xxx) 值如何,这都会根据需要删除 rgb 部分。

BUT!但! You wont be able to get initial as a result, because initial replaces to the default none value at text-decoration结果,您将无法获得initial ,因为initial替换为text-decoration处的默认none

 style = window.getComputedStyle(document.getElementById('gettextDecorationfrom')); weight = style.textDecoration; text = weight.replace(/?rgb\(([^;]*)\)/gm, ''); document.getElementById("results").innerText = text;
 .myclass { text-decoration:underline; }
 <div id="gettextDecorationfrom" class="myclass"> Test </div> <hr> Result: <div id="results"></div>

Per the docs, text-decoration is a shorthand property.根据文档, text-decoration 是一个速记属性。 See: https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration请参阅: https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration

That means it contains multiple properties.这意味着它包含多个属性。 Specifically, it contains text-decoration-line , text-decoration-color , text-decoration-style , and text-decoration-thickness .具体来说,它包含text-decoration-linetext-decoration-colortext-decoration-styletext-decoration-thickness That's why you see the RGB value, because it includes color in the text-decoration property.这就是您看到 RGB 值的原因,因为它在text-decoration属性中包含了颜色。

If you just want the text-decoration-line property, then you can use textDecorationLine , but that still will never give you initial as you mentioned in your first example.如果你只想要 text-decoration-line 属性,那么你可以使用textDecorationLine ,但它仍然不会像你在第一个例子中提到的那样给你initial

That's because getComputedStyle does not return what is in the CSS;那是因为getComputedStyle不返回 CSS 中的内容; it returns the style that actually ends up getting applied the element.它返回实际最终应用元素的样式。 That's why it's called computed style.这就是为什么它被称为计算风格。 initial is not a computed style, it's a value that instructs the browser to compute the style by using whatever the initial style was, which for text-decoration-line is none, as you can see here: https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration-line#formal_definition initial不是计算样式,它是一个值,指示浏览器使用任何初始样式来计算样式,对于 text-decoration-line 来说是 none,如您在此处看到的: https://developer.mozilla。 org/en-US/docs/Web/CSS/text-decoration-line#formal_definition

If you want to get that initial , you will have to actually inspect the stylesheet itself.如果你想得到那个initial ,你将不得不实际检查样式表本身。 See: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet请参阅: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet

Using a combination of both answers I was able to get what i needed.结合使用这两个答案,我能够得到我需要的东西。 The answer by Instance Hunter had the JavaScript textDecorationLine i was missing which allowed me to use FHJTDSDG's answer without the regex. Instance Hunter 的答案有 JavaScript 我缺少的textDecorationLine ,这让我可以在没有正则表达式的情况下使用 FHJTDSDG 的答案。

Working example..工作示例..

 updateresult() function toggleunderline() { if (weight === "none") { document.getElementById("gettextDecorationfrom").style.textDecorationLine = "underline" updateresult() } else { document.getElementById("gettextDecorationfrom").style.textDecorationLine = "initial" updateresult() } } function updateresult(){ style = window.getComputedStyle(document.getElementById('gettextDecorationfrom')); weight = style.textDecorationLine; document.getElementById("results").innerText = weight; }
 .myclass { text-decoration:initial; }
 <button onclick="toggleunderline()"> Toggle underline </button> <div id="gettextDecorationfrom" class="myclass"> Test </div> <hr> Result: <div id="results"></div>

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

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