gpt4 book ai didi

jquery - 在 JQuery 中将选择的背景颜色设置为选定的选项

转载 作者:技术小花猫 更新时间:2023-10-29 10:28:08 25 4
gpt4 key购买 nike

跟进这个问题:Setting background-color of select options in JQuery

我有一个包含多个选择框的页面,如下所示:

<select name="item-0-status" id="id_item-0-status">
<option value="">---------</option>
<option value="1">Online</option>
<option value="2">Offline</option>
<option value="3">Unknown</option>
</select>

这些是在 django 中自动生成的,因此不可能将 css 类、id 或属性直接应用于选项。选择元素的 ID 为“item-0-status”、“item-1-status”、“item-2-status”等。

我通过以下 JQuery 代码为选项分配颜色:

$('select[id$=-status][id^=id_item-]').children().each(
function (){
if($(this).val() == 0){
$(this).css('backgroundColor','white');
}
if($(this).val() == 1){
$(this).css('backgroundColor','green');
}
if($(this).val() == 2){
$(this).css('backgroundColor','red');
}
if($(this).val() == 3){
$(this).css('backgroundColor','orange');
}
}
);

效果很好。

我还希望选择的元素具有与所选选项相同的背景颜色,我正尝试使用以下方法实现这一点:

$('select[id$=-status][id^=id_item-]').each(
function (){
$(this).css('backgroundColor',$('option:selected',this).css('backgroundColor'));
}
);

但是,这只是将选择元素着色为蓝色(我认为它是从悬停属性而不是背景中获取颜色)。这是在 Firefox 3.6.8 中,就此问题而言,它是唯一相关的浏览器。

知道如何解决这个问题吗?

最佳答案

用“改变”替换“每个”

$('select[id$=-status][id^=id_item-]').change(
function (){
var color = $('option:selected',this).css('background-color');
$(this).css('background-color',color);
}
).change();

这适用于 Chrome。

另见:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions

支持在 django admin 中自定义 css。

而且我相信正确的 css 属性是:'background-color'backgroundColor 是 javascript,应该像这样使用:$(this).css({backgroundColor:color}); 但它似乎仍然有效,所以没什么大不了的。

编辑:

如果你想在页面加载时初始化脚本,你可以在后面添加 .change() 。见代码。

我还在 firefox 中对此进行了测试,我也看到了这种奇怪的行为(蓝色,蓝色?)。

另一个编辑:

好的,下面是 firefox 的快速修复:

$('select[id$=-status][id^=id_item-]').children().each(function (){
if($(this).val() == 0){
$(this).attr('style', 'background-color:white;');
}
if($(this).val() == 1){
$(this).attr('style', 'background-color:green;');
}
if($(this).val() == 2){
$(this).attr('style', 'background-color:red;');
}
if($(this).val() == 3){
$(this).attr('style', 'background-color:orange;');
}
});

$('select[id$=-status][id^=id_item-]').change(function (){
var style = $(this).find('option:selected').attr('style');
$(this).attr('style', style);
}).change();

上次编辑:

如果你使用的是 css,你甚至可以这样做:

<style>
select option,
select {
background-color:white;
}

select option[value="1"],
select.o1
{
background-color:green;
}

select option[value="2"],
select.o2
{
background-color:red;
}

select option[value="3"],
select.o3
{
background-color:orange;
}
</style>

<script>
$('select[id$=-status][id^=id_item-]').change(function (){
var color = $(this).find('option:selected').val();

$(this).removeClass('o1 o2 o3').addClass('o' + $(this).find('option:selected').val());
}).change();
</script>

另一个编辑:

我遇到了这个,发现我可以缩短它,所以我这样做只是为了好玩:

$('select[id$=-status][id^=id_item-]').children().each(function() {    
var colors = ['white', 'green', 'red', 'orange'];
$(this).attr('style', 'background-color:' + colors[$(this).val()] + ';');
});
$('select[id$=-status][id^=id_item-]').change(function() {
$(this).attr('style', $(this).find('option:selected').attr('style'));
}).change();

关于jquery - 在 JQuery 中将选择的背景颜色设置为选定的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5068087/

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