如何使用javascript更改选择2的背景颜色?

问题描述 投票:0回答:5

演示和我的代码是这样的:http://jsfiddle.net/fc1qevem/10/

我的javascript代码是这样的:

var select02 = $('#select02');

$(select02).select2({
    data: [{
        id: 0,
        text: "test01"
    }, {
        id: 1,
        text: "test02"
    }, {
        id: 2,
        text: "test03"
    }, {
        id: 3,
        text: "test04"
    }, {
        id: 4,
        text: "test05"
    }],
     placeholder: "Branch name",
});

我想将select2的背景颜色更改为蓝色

我想使用javascript更改背景颜色

是否可以做?

谢谢你

javascript jquery html css jquery-select2
5个回答
2
投票

将其添加到你的CSS中

.select2-selection{
  background-color:blue !important;
}

js解决方案

 $(select02).select2({
    data: [{
        id: 0,
        text: "test01"
    }, {
        id: 1,
        text: "test02"
    }, {
        id: 2,
        text: "test03"
    }, {
        id: 3,
        text: "test04"
    }, {
        id: 4,
        text: "test05"
    }],
     placeholder: "Branch name",
}).data("select2").$container.find(".select2-selection").css('background-color', 'blue');

0
投票

当您使用 JavaScript 更改背景颜色时,请尝试以下操作:

$('.select2-selection').css('背景颜色', '蓝色');


0
投票
// Css code
.bg_color {
     background-color: blue !important;
}

// Javascript code
// just put this code under the load function

$(select02).select2({ containerCssClass: "bg_color " });

这就是 select2 如何向容器添加类或 select2 的默认 css“.select2-container--default .select2-selection--single”,您可以在其中编辑容器的背景颜色。希望对你有帮助。


0
投票

我就是这样用的,效果很好:

$("#select02").siblings('.select2').children('.selection').children('.select2-selection').css('background-color', 'blue')

-1
投票

尝试以下代码:-

document.getElementById("select02").style.backgroundColor="blue";
© www.soinside.com 2019 - 2024. All rights reserved.