JS implements the method of switching the background color of a specified area by clicking the Color Block

Source: Internet
Author: User

JS implements the method of switching the background color of a specified area by clicking the Color Block

This article mainly introduces how to switch the background color of a specified area by clicking a Color Block in javascript. It involves the cookie and background color operations in javascript and has some reference value. For more information, see

 

 

This example describes how to switch the background color of a specified area by clicking a Color Block in JS. Share it with you for your reference. The specific implementation method is as follows:

 

The Code is as follows:

<Html>
<Head>
<Title> click the Color Block in JS to switch the background color of the specified area </title>
</Head>
<Body>
<Div align = "center">
<Table bgcolor = # F8F8F8 border = "0" width = "500" cellpadding = "0" style = "border-collapse: collapse "height =" 180 "id =" bb1 ">
<Tr>
<Td colspan = "2">
<Div align = "center">
<Table border = "0" width = "90%" cellpadding = "0" style = "border-collapse: collapse" height = "90" id = "table2">
<Tr> <td style = "line-height: 150%"> <span style = "font-size: 14px "> change the background color of the specified area of the webpage in a timely manner </span> </td>
</Tr> </table>
</Div>
</Td>
</Tr> </table> </div>
<Div align = "center">
<Table border = "0" width = "500" cellpadding = "0" style = "border-collapse: collapse" height = "20" id = "table1">
<Tr> <td width = "219"> <p align = "right"> <font color = "#808080"> <span style = "font-size: 9pt "> select the background: </span> </font> </td>
<Td width = "281"> <div align = "center"> <table border = "1" width = "240" cellspacing = "0" cellpadding = "0" height = ""20" id = "table1" style = "border-collapse: collapse "bordercolor =" # FFFFFF ">
<Tr>
<Td width = "60" onClick = "bb1.style. backgroundColor = 'fdfdf0 '; set_color ('fdfdf0')" bgcolor = "# FDFDF0"> </td>
<Td onClick = "bb1.style. backgroundColor = 'b1d5f3 '; set_color ('b1d5f3')" bgcolor = "# B1D5F3" width = "60"> </td>
<Td onClick = "bb1.style. backgroundColor = 'b4e7d9'; set_color ('b4e7d9')" bgcolor = "# B4E7D9" width = "60"> </td>
<Td onClick = "bb1.style. backgroundColor = 'f1e8ff'; set_color ('f1e8ff')" bgcolor = "# F1E8FF" width = "60"> </td>
<Td onClick = "bb1.style. backgroundColor = 'e8fff3 '; set_color ('e8fff3')" bgcolor = "# E8FFF3" width = "60"> </td>
<Td onClick = "bb1.style. backgroundColor = 'cdcdde'; set_color ('cdcdde')" bgcolor = "# CDCDDE" width = "60"> </td>
</Tr>
</Table>
</Div> </td> </tr> </table>
</Div>
<Script language = javascript>
Function get_cookie (name_to_get ){

 

Var cookie_pair
Var cookie_name
Var cookie_value
Var cookie_array = document. cookie. split (";")
For (counter = 0; counter <cookie_array.length; counter ++ ){
Cookie_pair = cookie_array [counter]. split ("= ")
Cookie_name = cookie_pair [0]
Cookie_value = cookie_pair [1]
If (cookie_name = name_to_get ){
Return unescape (cookie_value)
}
}
Return null
}
Var bg_color = get_cookie ("bgColor_cookie ")
Function set_color (color_val ){
Set_cookie ("bgColor_cookie", color_val, 365 ,"/")
}

Function set_cookie (cookie_name, cookie_value, cookie_expire, cookie_path, cookie_domain, cookie_secure ){
Var cookie_string = cookie_name + "=" + cookie_value
If (cookie_expire ){
Var expire_date = new Date ()
Var ms_from_now = cookie_expire * 24x60*60*1000
Expire_date.setTime (expire_date.getTime () + ms_from_now)
Var expire_string = expire_date.toGMTString ()
Cookie_string + = "; expires =" + expire_string
}

If (cookie_path ){
Cookie_string + = "; path =" + cookie_path
}
If (cookie_domain ){
Cookie_string + = "; domain =" + cookie_domain
}
If (cookie_secure ){
Cookie_string + = "; true"
}
Document. cookie = cookie_string
}
If (bg_color ){
Bb1.style. backgroundColor = bg_color
}
</Script>
</Body>
</Html>

 

I hope this article will help you design javascript programs.

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.