jquery Gets the background Color property value/color value in the style _jquery

Source: Internet
Author: User
Days using jquery to get the value of the Background-color in the style found that the color value in IE is not the same as the Chrome, Firefox display format, ie in the hex format display #ffff00, and Chrome, Firefox is in the GRB format to display RGB (255,0,0), because the need to store color values in the database, so you want to make the color value of the format unified (in fact, is not unified also can be saved). Search for a bit, from a foreign website to get a section of code
Copy Code code as follows:

$.fn.gethexbackgroundcolor = function () {
var RGB = $ (this). CSS (' Background-color ');
RGB = Rgb.match (/^rgb (d+), s* (d+), s* (d+)) $/);
function Hex (x) {return ("0" + parseint (x). toString). Slice (-2);}
return rgb= "#" + Hex (rgb[1]) + hex (rgb[2]) + hex (rgb[3));
}

Solutions
The above definition is a jquery function that we can pass $ ("#bg"). Gethexbackgroundcolor (); Gets the RGB value of the Background-color of the label id= "BG"

Below do a little modification, is to add a judgment, if it is IE browser directly to get the value, if the non-IE browser will convert the value to RGB format:
Copy Code code as follows:

$.fn.gethexbackgroundcolor = function () {var RGB = $ (this). CSS (' Background-color '); if (!$.browser.msie) {RGB = Rgb.match (/^rgb\ (\d+), \s* (\d+), \s* (\d+) \) $/); function Hex (x) {return ("0" + parseint (x). toString). Slice ( -2);} rgb= "#" + Hex (rgb[1)) + hex (rgb[2)) + hex (rgb[3)); } return RGB; }

jquery Gets the value of a property in a style sheet
Copy Code code as follows:

<script type= "Text/javascript" src= "Jquery-1.3.2.min.js" ></script>
<script type= "Text/javascript" >
$ (function ()
{
$ (". One > A"). Click (function () {
<span style= "COLOR: #ff0000" >var ba=$ (This) [0].style.background;</span> alert ($ (this). html () +ba);
if (ba== "url (jia1.jpg) no-repeat")
{$ (this). CSS ({background: "url (jia.jpg) no-repeat"});
Else
$ (this). CSS ({background: "url (jia1.jpg) no-repeat"});
});
});
</script>
<style type= "Text/css" >
#left. One a{background:url (jia1.jpg) no-repeat;
padding-left:18px;
padding-top:5px;
Cursor:hand;
}

</style>
<body>
<div id= "Left" >

<ul>
<li class= "One" > <a> Information Center </a>
<ul>
<li class= "Two" > <a href= "#" > Information? Box </a> </li>
<li class= "Two" > <a href= "#" > Information inbox </a> </li>
</ul>
</li>
<li class= "One" > <a> I'm going to pick it up? </a> </li>
</ul>
</div>
</body>

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.