繁体   English   中英

使用JavaScript更改元素单独的样式背景颜色

[英]Changing element individual style background-color with javascript

我要更改的元素

<button id="thumb" onfocus="focus()" style="height: 12px; width: 12px; background-color: white; border-radius: 50%; margin: 0 auto; position: relative; top: -3px; left: 50%; box-shadow: 0px 0px 3px #888888;">
</button>

函数调用:

function focus() {
    document.getElementById('thumb').style.background-color="blue";
}

错误:

Uncaught ReferenceError: Invalid left-hand side in assignment

我不明白为什么这是一个错误。 我认为可能是因为-符号。 它可以与其他样式一起使用,例如document.getElementById('thumb').style.width="10px"可以使用。

对于虚线样式,您需要使用camelCase 所以用

-lowerCase = Uppercase

例如

background-color : backgroundColor;
font-size : fontSize


 function focus() {
     document.getElementById('thumb').style.backgroundColor="blue";
 }

尝试这个

document.getElementById('thumb').style.backgroundColor="blue";

替代使用JavaScript的另一种方法是使用CSS更改焦点上按钮的颜色。

我在下面提供了一个示例,希望对您有所帮助。

的CSS

.item-input:focus {
   background-color: blue;
}

CSS CLASS:确保包含class =“ item-input”

<button id="thumb" class="item-input" onfocus="focus()" style="height: 12px; width: 12px; background-color: white; border-radius: 50%; margin: 0 auto; position: relative; top: -3px; left: 50%; box-shadow: 0px 0px 3px #888888;">
</button>

暂无
暂无

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

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