Home > Web Front-end > JS Tutorial > Problem with JQuery getting the background-color color value in the style_jquery

Problem with JQuery getting the background-color color value in the style_jquery

WBOY
Release: 2016-05-16 17:25:02
Original
1571 people have browsed it

Today, when I used JQuery to get the value of background-color in the style, I found that the format of the obtained color value in IE is different from that displayed in Chrome and Firefox. In IE, it is displayed in HEX format [#ffff00], while in Chrome and Firefox It displays [rgb(255,255,0)] in GRB format, and what I need is a hexadecimal color value for storage in the database. I found the following code on the Internet to solve this problem:

Copy code The code is 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(16)).slice(-2);
}
rgb= "#" hex(rgb[1]) hex(rgb[2]) hex(rgb[3]);
}
return rgb;
}

In this way, everything is displayed in HEX format and has been verified.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template