如何更改(并保持)单击按钮的颜色 [英] how to change (and keep) the color of a button on click
问题描述
我想在用户单击时切换按钮的颜色.现在,当用户单击按钮时,颜色会暂时更改,但立即以毫秒为单位更改回第一种颜色.我使用.jsp文件通过这些按钮单击来与服务器通信,这会将信息发送到服务器,并且当服务器通信回来时,它刷新了页面.页面刷新时,这是否重新设置了按钮的样式?我正在使用类按钮"来定义样式.
I would like to toggle the color of a button upon a user click. Right now, when the user clicks the button momentarily changes color but then immediately changes back to the 1st color in milliseconds. I am using a .jsp file to communicate to a server on these button clicks which sends information to the server, and when the server communicates back it refreshed the page. Is this re-setting the style of the button when the page refreshes? I am using a class 'button' to define the style.
我的问题是:如何无限期更改按钮的颜色?
My question is: how can I indefinitely change the color of the button?
<form action="FirstServlet" method="get">
<div id="TVs">
<%for (int i=1; i<=numTargets; i++) {
<button id="TV<%=i%>" name= "TV<%=i%>" class="button" onClick= "TVbuttonPressed('TV<%=i%>')">
...
function TVbuttonPressed(id){
document.getElementById(id).style.backgroundColor = 0xFFFF00;
}
最终我打算将功能设置为:
Eventually I am planning to have the function be:
function TVbuttonPressed(id){
if (document.getElementById(id).style.backgroundColor == A)
document.getElementById(id).style.backgroundColor = B
else (document.getElementById(id).style.backgroundColor == B)
document.getElementById(id).style.backgroundColor = A
}
有人可以帮忙解释一下吗?谢谢.
Can someone help explain? Thanks.
推荐答案
将return false放置在被调用的函数中
Place the return false in the called function
function TVbuttonPressed(id){
document.getElementById(id).style.backgroundColor = 0xFFFF00;
return false; --this will prevent the change of color while refreshing.
}
它将起作用
这篇关于如何更改(并保持)单击按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!