JQuery gets the background color attribute value/color value in the style _ jquery

Source: Internet
Author: User
When Angel uses jQuery to obtain the value of background-color in the style, the obtained color value is displayed in a different format than that displayed in Chrome and Firefox. in IE, the color value is displayed in HEX format # ffff00, chrome and Firefox display rgb (, 0, 0) in GRB format, because the color value needs to be stored in the database, therefore, we want to unify the color value format (which is actually not uniform ). Search, get a piece of code from a foreign website. Angel uses jQuery to get the value of background-color in the style. The color value obtained now is in a different format than that displayed in Chrome and Firefox in IE, in IE, # ffff00 is displayed in HEX format, while in Chrome and Firefox, rgb (255, 0, 0) is displayed in GRB format. Because color values need to be stored in the database, therefore, we want to unify the color value format (which is actually not uniform ). I searched and got a piece of code from a foreign website.

The Code is as follows:


$. Fn. getHexBackgroundColor = function (){
Var rgb = background (this).css ('background-color ');
Rgb = rgb. match (/^ rgb (d +), s * (d +), s * (d +) $ /);
Function hex (x) {return ("0" + parseInt (x). toString (16). slice (-2 );}
Return rgb = "#" + hex (rgb [1]) + hex (rgb [2]) + hex (rgb [3]);
}


Solution
The above defines a jQuery function. We can use $ ("# bg"). getHexBackgroundColor (); to obtain the RGB value of background-color with the tag id = "bg ".

The following is a small modification, that is, adding a judgment. If it is an IE browser, the value will be taken directly. If it is not an IE browser, the value will be converted to the RGB format:

The Code is as follows:


$. Fn. getHexBackgroundColor = function () {var rgb = partition (this).css ('background-color'); if (! $. Browser. msie) {rgb = rgb. match (/^ rgb \ (\ d +), \ s * (\ d +), \ s * (\ d +) \) $ /); function hex (x) {return ("0" + parseInt (x ). toString (16 )). slice (-2);} rgb = "#" + hex (rgb [1]) + hex (rgb [2]) + hex (rgb [3]);} return rgb ;}


JQuery obtains the attribute values in the style sheet.

The Code is as follows:



Aaaa

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.