【发布时间】:2018-01-20 05:13:06
【问题描述】:
我有两个使用相同 JS 的引导选择表单。它是两个颜色名称列表,样式显示在用 CSS 类描述的颜色中。
我已经将 CSS 类名也存储在每个选项元素的值中,以便 JS 可以访问它,因此它可以在单击前后保持正确的颜色。
我正在尝试通过使用“addClass”和“removeClass”添加和删除类来更改颜色,但 removeClass 不起作用。这意味着您可以向下单击各种选项,显示的选项将以正确的颜色显示,但是当您返回并选择先前单击的选项时,它不会返回到该颜色。
谁能解释一下这个问题?
作为JSFiddle
HTML
<div class="selectContainer">
<select class="form-control pickerSelectClass" id="select_1">
<option class="big _21" value="_21">■ Bright Red</option>
<option class="big _106" value="_106">■ Bright Orange</option>
<option class="big _24" value="_24">■ Bright Yellow</option>
</select>
<select class="form-control pickerSelectClass" id="select_2">
<option class="big _21" value="_21">■ Bright Red</option>
<option class="big _106" value="_106">■ Bright Orange</option>
<option class="big _24" value="_24">■ Bright Yellow</option>
</select>
</div>
CSS
.selectContainer {width:200px}
.big {
font-size: 16px;
font-weight: bold !important;
text-shadow: -1px -1px 0 #444, 1px -1px 0 #444, -1px 1px 0 #444, 1px 1px 0 #444;
}
._21 {
color: red !important
}
._106 {
color: orange !important
}
._24 {
color: yellow !important
}
JS
$(document).ready(function() {
$(".pickerSelectClass").selectpicker();
$('select').each(function(index, item){
$(this).parent().find('.filter-option').addClass("big");
$(this).parent().find('.filter-option').addClass($(this).val());
}).on('change', function(){
$(this).parent().find('.filter-option').removeClass('_*')
$(this).parent().find('.filter-option').addClass($(this).val());
});
});
外部资源
jquery.min.js,
bootstrap.css,
bootstrap.min.js,
bootstrap-select.css,
bootstrap-select.min.js
【问题讨论】:
-
您应该检查您在视觉上看到的页面上的选择元素。您似乎正在尝试更改页面上的
-
它确实说“选择”,但随后它会搜索“.filter-option”,实际上是在使用它而不是默认值。我可以看到,当我单击它时,我得到了我想要的行为,当我检查它时,我发现它正在使用 booststrap 类“.filter-option”,所以它不是你提到的本机隐藏元素。就像我说的,我使用相同的机制来尝试添加和删除类。添加作品,所以我不明白为什么删除不起作用。
-
我很困惑,好像我在某些示例中查看silviomoreto.github.io/bootstrap-select/examples,我看到的可见选择元素实际上是一个跨度。当我单击带有选项的菜单时显示的菜单是带有 lis 的无序列表。当您检查页面上的可见元素时,不是您所说的原生选择。
标签: javascript jquery html css twitter-bootstrap