单击之后,如何更改按钮的背景颜色?
我需要使用以下代码将按钮的背景颜色从深色更改为
<button class="btn btn-dark" id="btn" th:attr="onclick=|vote('${listAnswer.answer_id}','+1')|"><i class="fa fa-star fa-2x" ></i>UpVote</button>
绿色。它不起作用。请帮我
function vote(answerId,rateValue){
alert(rateValue);
axios.post('/saveRatings?answerId=' + answerId + '&rateValue=' + rateValue, {
})
.then(function(response){
document.getElementById("btn").style.background-color='green';
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JavaScript属性名称不能包含
-
字符,因为这是操作员。因此,必须使用
而不是:
使用此:
JavaScript property names cannot contain the
-
character because this is an operator.Due to this, CSS properties must be accessed with camelCase syntax.
Instead of :
Use this :
您可以尝试使用
JS
You can try this way
JS
单击按钮后,状态将变为“活动”,您可以使用CSS将按钮的颜色更改为您想要的任何内容。您需要做的就是使用“:Active”选择器添加CSS规则。
https://www.w3schools.com/csssssref/csssref/cssref/sel_active.asp
基本上:
After you have clicked a button it's state becomes "active" and you can use css to change the color of the button to whatever you want. All you need to do is add a css rule using the ":active" selector.
https://www.w3schools.com/cssref/sel_active.asp
https://www.freecodecamp.org/news/css-button-style-hover-color-and-background/
Basically:
另外,代替
document.getElementById(“ btn”)。样式。background-color
您可以使用document.getElementById(“ btn”)。样式['backicken-codor']
also instead of
document.getElementById("btn").style.background-color
you can usedocument.getElementById("btn").style['background-color']