我正在使用 Select2 在我的应用程序中设置多选标签类型字段。我知道哪个 CSS 属性会在将选择框放入选择字段后更改其颜色:
选择框颜色
.select2-container--default .select2-selection--multiple .select2-selection__choice {
background-color: #e4e4e4;
border: 1px solid #aaa;
border-radius: 4px;
cursor: default;
float: left;
margin-right: 5px;
margin-top: 5px;
padding: 0 5px
}
我想要弄清楚的是如何根据用户所做的选择进行此更改。选择列表是预先定义的。
尝试过这样的事情:
JavaScript 初始化/更改框颜色
$(document).ready(function () {
$('#test').select2();
$('.select2-container--default .select2-selection--multiple .select2-selection__choice').each(function () {
var title = $(this).attr('title');
console.log(title);
if (title === 'Breakfast') {
$(this).parent().css({ 'background-color': "green" });
}
if (title === 'Brunch') {
$(this).parent().css({ 'background-color': "red" });
}
if (title === 'Lunch') {
$(this).parent().css({ 'background-color': "blue" });
}
});
});
这似乎不起作用。有人尝试过为 Select2 设置标签框样式吗?
Change $(this).parent().css('background-color', 'green');
by $(this).parent().css({ 'background-color': "green" });
其余的也一样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)