如何更改(并保留)按下按钮的颜色?

时间:2022-11-20 14:03:03

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.

我想在用户单击时切换按钮的颜色。现在,当用户单击按钮时,会立即更改颜色,但随后立即以毫秒为单位更改为第一种颜色。我正在使用.jsp文件与服务器进行通信,单击这些按钮将向服务器发送信息,当服务器进行通信时,刷新页面。当页面刷新时,是否重新设置按钮的样式?我正在使用一个类“按钮”来定义样式。

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.

有人能帮助解释吗?谢谢。

1 个解决方案

#1


0  

Place the return false in the called function

在被调用的函数中放置返回false

function TVbuttonPressed(id){       
document.getElementById(id).style.backgroundColor  = 0xFFFF00;

return false;  --this will prevent the change of color while refreshing.

}

}

it will work

它将工作

#1


0  

Place the return false in the called function

在被调用的函数中放置返回false

function TVbuttonPressed(id){       
document.getElementById(id).style.backgroundColor  = 0xFFFF00;

return false;  --this will prevent the change of color while refreshing.

}

}

it will work

它将工作