JavaScript dynamic Change button style __java

Source: Internet
Author: User

Mainly used to button onmouseover, onmouseout, onclick events, the mouse moved to the button, the transparency of the button changes, remove the button transparency recovery. Click the button, the button is clicked background color change, click the Next button the button background color change, the previous button background color recovery. The effect is as above, the code is as follows. For more information, see Code Comments:

Place the following code in the head area:

<script language=javascript>
var prebuttonid= "null"; variable is used to store the ID of the last button being clicked and to define the initial value as null

function Buttonmouseover (ButtonID) {
document.getElementById (ButtonID). filters.alpha.opacity=60;
Change the transparency of the corresponding ID button when you move the mouse
}
function Buttonmouseout (ButtonID) {
document.getElementById (ButtonID). filters.alpha.opacity=100;
Change the transparency of the corresponding ID button when the mouse moves out
}
function Buttonmouseclick (ButtonID) {
if (prebuttonid!= "null") {//If the ID of the previous button is not an initial value null, the background color of the previous push is restored
document.getElementById (Prebuttonid). style.backgroundcolor= "#CCCCCC";
}
document.getElementById (ButtonID). style.backgroundcolor= "#FF0000";
Change the background color of the clicked button
PREBUTTONID=BUTTONID;//assigns the ID of the clicked button to the "Previous button" variable
}
</script>
<style type= "Text/css" >
. myinput{
Filter:alpha (opacity=100); * * This sentence can not be deleted, if not, JS can not control the transparency of the button * *
border:0px;
Background-color: #CCCCCC;
Font-family:sans-serif, Arial, Helvetica;
}
</style>

The following code is placed in the body area:

<input Type=button value=button1 name=button1 id=button1 class=myinput "onclick= (ButtonMouseClick)" Onmouseover= "Buttonmouseover (this.id)" onmouseout= "Buttonmouseout (this.id)" >
<input Type=button value=button2 name=button2 id=button2 class=myinput "onclick= (ButtonMouseClick)" Onmouseover= "Buttonmouseover (this.id)" onmouseout= "Buttonmouseout (this.id)" >
<input Type=button value=button3 name=button3 id=button3 class=myinput "onclick= (ButtonMouseClick)" Onmouseover= "Buttonmouseover (this.id)" onmouseout= "Buttonmouseout (this.id)" >
<input Type=button value=button4 name=button4 id=button4 class=myinput "onclick= (ButtonMouseClick)" Onmouseover= "Buttonmouseover (this.id)" onmouseout= "Buttonmouseout (this.id)" >
<input Type=button value=button5 name=button5 id=button5 class=myinput "onclick= (ButtonMouseClick)" Onmouseover= "Buttonmouseover (this.id)" onmouseout= "Buttonmouseout (this.id)" >

You can manually set the transparency and click on the background color, is a good effect.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.