我在我的網站上有一個日曆,打開顯示RGB顏色選擇器的彈出框。除IE8以外,它在所有瀏覽器中都可以正常工作。我的代碼如下。無法獲取未定義或空引用的屬性'1'
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
這是錯誤
SCRIPT5007:未定義或空引用無法獲取財產 '1'
指向此行
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
我我不熟悉這段代碼,因爲它是由我的工作場所中有其他人完成的度假幾個星期。如果有人能幫助我指出正確的方向,將不勝感激。
編輯
這是調用函數的地方。當我在IE8 console.log(bgColor)它顯示爲#rgb(238,238,238),所以我不明白爲什麼它不工作。也許這是我的愚蠢,但對我來說,我看不到它。
$('.period_day').click(function(e) {
var bgColor = $(this).css('background-color');
if (rgb2hex(bgColor) == "#eeeeee") {
$('.colour_picker').css('background-color', '#ff0000');
$('#cal_colour_val').val('#ff0000');
} else {
$('.colour_picker').css('background-color', bgColor);
$('#cal_colour_val').val(bgColor);
}
});
如果'bgColor'已經是十六進制('#eeeeee'),它應該很明顯,爲什麼當它傳遞到'rgb2hex'時不起作用。 – Bergi
抱歉,console.log(bgColor)顯示爲rgb(238,238,238)。它已經通過rgb2hex它是#eeeeee – Pooshonk
@Pooshonk做'console.log(bgColor)'在Internet Explorer *中顯示結果*具體是什麼?不同的瀏覽器報告顏色不同。 – Pointy