js获取当前对象的颜色判断改变颜色
function toHex(N) { if (N==null) return "00"; N=parseInt(N); if (N==0 || isNaN(N)) return "00"; N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N); return "0123456789abcdef".charAt((N-N%16)/16) + "0123456789abcdef".charAt(N%16); //小写 // return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16); //大写 } function RGBtoHEX(str){ if (str.substring(0, 3) == 'rgb') { var arr = str.split(","); var r = arr[0].replace('rgb(','').trim(), g = arr[1].trim(), b = arr[2].replace(')','').trim(); var hex = [ toHex(r), toHex(g), toHex(b) ]; return "#" + hex.join(''); }else{ return str; } }
使用时:
var color = RGBtoHEX($('.whitelists_no').css("backgroundColor")); if(color == "#cccccc"){ alert(123); return false; } else { alert(456); return false; }