我爲了得到CSS值的屬性正在使用window.getComputedStyle()
:的getComputedStyle()返回在FF空字符串時,而不是克羅姆返回一個計算值
- borderRadius
- 的borderStyle
- 邊框寬度
- BORDERCOLOR
我注意到最新的FF返回空字符串,而是返回計算結果也有單位值:
FF:
"borderRadius":"","borderStyle":"","borderWidth":"","borderColor":""
鉻:
"borderRadius":"0px","borderStyle":"none","borderWidth":"0px","borderColor":"rgb(0, 0, 0)"}
我想知道:
- 這種差異造成了已知的bug?
- 你知道一種強制FF以Chrome的形式返回值的方法嗎? (我知道我可以在某些條件下添加默認值,但如果可能的話我會使用本地解決方案)。
(function (window) {
document.addEventListener('DOMContentLoaded', (event) => {
let elmTarget = document.querySelector('#target'),
elmResult = document.querySelector('#result');
let styles = window.getComputedStyle(elmTarget),
result = {
borderRadius: styles.borderRadius,
borderStyle: styles.borderStyle,
borderWidth: styles.borderWidth,
borderColor: styles.borderColor
},
resultStr = JSON.stringify(result);
console.log(resultStr);
elmResult.innerHTML = resultStr;
});
})(window);
#target {
background-color: blue;
width: 100px;
height: 50px;
}
<div id="target"></div>
<div id="result"></div>
謝謝....但怎麼樣的borderStyle,邊框寬度,邊框顏色? – GibboK
@GibboK,same:'borderLeftColor'等我正在寫一個片段與所有。半徑更加複雜('borderTopLeftRadius') – Kaiido
感謝您的評論,我還在FF中添加了關於此行爲原因的更多信息。 – GibboK