gpt4 book ai didi

javascript - 无法获取未定义或空引用的属性 '1'

转载 作者:行者123 更新时间:2023-11-30 17:53:37 27 4
gpt4 key购买 nike

我的网站上有一个日历,它会打开一个显示 RGB 颜色选择器的弹出框。它在除 IE8 之外的所有浏览器中都可以正常工作。我的代码如下。

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: Unable to get property '1' of undefined or null reference

指向这条线

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);
}
});

最佳答案

该错误意味着 rgb2hex 函数正在使用与正则表达式不匹配的参数调用。匹配失败将导致变量 rgb 为空,并且该代码不会检查这种可能性。

因此,真正的问题可能在于调用函数的代码。

关于javascript - 无法获取未定义或空引用的属性 '1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18469805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com