gpt4 book ai didi

javascript - 应该是未定义的 jQuery 对象

转载 作者:行者123 更新时间:2023-11-30 08:52:25 26 4
gpt4 key购买 nike

我正在尝试使用 jQuery 更改背景颜色。我制作了一个包含颜色名称及其值的数组。

如果我这样做

console.log(colorValue);

当我这样做时,我得到了我想用来找到相应颜色的类名

console.log(colorArr.colorValue);

我得到一个未定义的,但是当我这样做的时候

console.log(colorArr.red);

我得到相应的颜色:

function colorChange(){
$('div.colorpicker ul li a').on('click', function() {
var colorArr = {'greenyellow': '#d2db46', 'lightgreen': '#8dc13f', 'darkgreen': '#56a174', 'blauw' : '#3199d1', 'darkblue':'#326b9b', 'darkpurple':'#584586' , 'purple':'#985494', 'red':'#ca4538', 'orange' : '#e27a37', 'darkyellow': '#f8c040', 'lightyellow': '#e4de42'};
var changeBackground = $('div.header_blue, div.reactie, section#adres, section#referenties_single div.pager');
var changeColor = $('section#tevredenklanten h1, section#referenties_single h2.klant, section#referenties_single .wat_gedaan h2');
var colorValue = $(this).attr('class');
console.log(colorValue);
//console.log(colorArr);
console.log(colorArr.colorValue);
//console.log(colorArr.red);
changeBackground.animate({backgroundColor:colorArr.colorValue}, 600);
});
}

有人知道我必须做什么吗?

最佳答案

由于 colorValue 不是对象键,但它包含要查找的键,因此您需要使用 [] 表示法而不是 符号

console.log(colorArr[colorValue]);

当你说 colorArr.colorValue 时,它会在对象 colorArr 中寻找一个名为 colorValue 的键,但它不存在,因此它返回 未定义

关于javascript - 应该是未定义的 jQuery 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16692175/

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