我在HTML页面上有一个
此选项使用selected
属性,并使用jQuery。这是一个示例的fiddle:JsFiddle
<select>
<option>test1</option>
<option selected disabled>test2</option>
<option>test3</option>
<option>test4</option>
</select>
$('select > option:selected:disabled').each(function(){
$(this).parent().css("color","red");
});
$('select').change(function(){
$(this).css("color", "black");
});
/*reset color for options*/
select option:not(:disabled) {color:black;}
.css()
。如果我已经在使用jQuery,那么有什么优势呢?我有点理解...如果可以完全避免使用jQuery,那么这样做可能是有意义的,但这不是你建议的。我认为将其设置为初始值可能比使用黑色更好,但我想象这将用作验证错误...在这种情况下,我会切换一个类(可能使用jQuery):P - Grayvar elem = document.getElementById('idOfYourSelect');
var selectedOption= elem[elem.selectedIndex];
if(selectedOption.disabled == true){
elem.style.color='red'; // apply the styles
}
并在选择它时将其恢复为正常状态(如果您想这样做)
elem.onclick= function(){
elem.style.color='initial'; // revert back to normal style
}
请查看这个fiddle。
有一种纯CSS解决方案,采用CSS :has()选择器,现在被广泛应用于Web浏览器中。
select option {
color: black;
}
select option:disabled {
color: grey;
}
select:has(option:checked:disabled) {
color: red;
}
<select>
<option value="1">apples</option>
<option value="2">oranges</option>
<option value="3" disabled selected>bananas</option>
</select>
以下是使用jQuery实现的简单示例:
<select>
<option>test1</option>
<option selected disabled>test2</option>
<option>test3</option>
<option>test4</option>
</select>
$('select > option:selected:disabled').each(function() {
var select = this.parentNode;
select.classList.add("select-with-disabled-selected-option");
$(select).change(function() {
this.classList.remove("select-with-disabled-selected-option");
});
});
.select-with-disabled-selected-option {
color: red;
}
我使用jQuery使它更简单。
但是http://youmightnotneedjquery.com/。
谢谢大家。最终我做了以下的事情,在很多答案和评论的帮助下。
<script type="text/html" id="fkeyByName-template">
<select data-bind="optionsCaption: 'Choose...',
optionsText: function(item){ return item.value.name;},
optionsValue: function(item){ return item.id;},
valueAllowUnset: true,
options: (function(){return $root.foreignKeys.values(col.ftype);})(),
value: value,
optionsAfterRender:function(option,item){
$root.setOptionDisable(option,item,value,$element,$data);
}
">
</select>
{{! <p data-bind="text: value"></p> }}
</script>
<script type="text/javascript">
model.setOptionDisable = function (option, item, value, select, data) {
if (item) {
ko.applyBindingsToNode(option, {disable: item.value.removed}, item);
if (value() == item.id) {
ko.applyBindingsToNode( select,{css: {disabledSelected: item.value.removed}}, data);
}
select.onchange = function() {
ko.applyBindingsToNode( select,{css: {disabledSelected: false}}, data);
};
}
}
</script>
<style>
select option:disabled, select.disabledSelected
{
color: red;
}
/*reset color for options*/
select option:not(:disabled)
{
color:initial;
}
</style>
ko
是什么?它有什么作用..?! 假设您将此作为答案发布给其他人的问题,他们是否能理解任何内容..? 顺便说一下,通过收集所有答案来发布自己的答案,而不是选择帮助您找到解决方案的答案,真的很令人沮丧。我不认为这就是 SO 的工作方式。但很高兴知道答案激发了您的灵感~! - T J这可以用 jQuery 实现。
想象一下你有这个:
<select class="sel">
<option disabled="true">one</option>
<option>two</option>
<option disabled="true">three</option>
<option>four</option>
</select>
在加载函数中,你可以放置以下代码:
$('.sel option[disabled="true"]').each(function () {
$(this).attr('style', 'color:red');
});
$(this).parent.css("color","red")
。 - Gray!select option:selected:disabled
,我相信(假设你的选项有selected属性)。 - Gray