當要透過Jquery 取得 Background 的顏色,取出來的結果都是RGB的數值,當想要呈現為HEX 值時
可以透過這一個外掛,直接轉換RGB為HEX。
程式碼如下:
xxxxxxxxxx
// 取得 BackgroundColor to Hex
(function($){
$.cssHooks.backgroundColor = {
get: function(elem) {
if (elem.currentStyle)
var bg = elem.currentStyle['background-color'];
else if (window.getComputedStyle)
var bg = document.defaultView.getComputedStyle(elem,
null).getPropertyValue('background-color');
if (bg.search('rgb') == -1)
return bg;
else {
bg = bg.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ('0' + parseInt(x).toString(16)).slice(-2);
}
return '#' + hex(bg[1]) + hex(bg[2]) + hex(bg[3]);
}
}
}
}(jQuery));