我已经创建了一个简单的 html 页面,其中包含一个按钮,该按钮在单击时会改变其颜色:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button onclick="if (this.style.backgroundColor == 'Red') {
this.style.backgroundColor = 'Green'
}
else {
this.style.backgroundColor = 'Red'
}
alert(this.style.backgroundColor)">
ok
</button>
</body>
</html>
令我惊讶的是 alert(this.style.backgroundColor)
返回 red 而不是 Red。 为什么?有一个赋值 this.style.backgroundColor = 'Red'
并且在这个 Red 中以大写字母开头。
最佳答案
CSS 样式不会保存为文字字符串,它们会转换为样式的内部规范表示。颜色样式不区分大小写,规范表示为小写。
关于javascript - 为什么 javaScript 中的字符串赋值不区分大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23215963/