你知道当复选框被禁用时如何设置其样式吗?
例如:
<input type="checkbox" value="All Terrain Vehicle"
name="exfilter_All Terrain Vehicle"
id="exfilter_All_Terrain_Vehicle"
class="exfilter" disabled="">
input[disabled]{
outline:1px solid red; // or whatever
}
仅用于复选框
input[type=checkbox][disabled]{
outline:1px solid red; // or whatever
}
$('button').click(function() {
const i = $('input');
if (i.is('[disabled]'))
i.attr('disabled', false)
else
i.attr('disabled', true);
})
input[type=checkbox][disabled] {
outline: 2px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" value="tasd" disabled />
<input type="text" value="text" disabled />
<button>disable/enable</button>
由于禁用复选框受浏览器/操作系统控制,因此无法直接对其进行样式设置。
但是,您可以巧妙地使用纯CSS将复选框替换为模拟复选框的标签。您需要有一个相邻的标签,以便您可以使用它来样式化新的“伪复选框”。基本上,您完全重新绘制了它,但这使您可以完全控制其在任何状态下的外观。
我提供了一个基本示例,以便您可以看到它的效果:http://jsfiddle.net/JohnSReid/pr9Lx5th/3/
这是示例:
input[type="checkbox"] {
display: none;
}
label:before {
background: linear-gradient(to bottom, #fff 0px, #e6e6e6 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
border: 1px solid #035f8f;
height: 36px;
width: 36px;
display: block;
cursor: pointer;
}
input[type="checkbox"] + label:before {
content: '';
background: linear-gradient(to bottom, #e6e6e6 0px, #fff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
border-color: #3d9000;
color: #96be0a;
font-size: 38px;
line-height: 35px;
text-align: center;
}
input[type="checkbox"]:disabled + label:before {
border-color: #eee;
color: #ccc;
background: linear-gradient(to top, #e6e6e6 0px, #fff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
}
input[type="checkbox"]:checked + label:before {
content: '✓';
}
<div><input id="cb1" type="checkbox" disabled checked /><label for="cb1"></label></div>
<div><input id="cb2" type="checkbox" disabled /><label for="cb2"></label></div>
<div><input id="cb3" type="checkbox" checked /><label for="cb3"></label></div>
<div><input id="cb4" type="checkbox" /><label for="cb4"></label></div>
根据您的浏览器兼容性和可访问性水平,可能需要进行一些额外的微调。
input[disabled] { /* css attributes */ }
input[disabled]
,这样它只会检查是否存在禁用属性。 - stevelove<select>
)是操作系统级别的组件,而不是浏览器级别的。您无法可靠地以一种在各种浏览器和操作系统中保持一致的方式对它们进行样式化。:disabled
选择器(适用于CSS3):checkbox-style { }
checkbox-style:disabled { }
或者你需要使用JavaScript根据启用/禁用状态来更改样式(假设它是基于你的问题被启用/禁用的)。
input[type="checkbox"] {
pointer-events: none;
}
如果你想阻止某人更新复选框以使其看起来被禁用,那么只需使用JQuery。
$('input[type=checkbox]').click(false);
然后你可以自定义样式复选框。
<input type='checkbox' onclick='return false;' />
- S.Masoninput[type='checkbox'][disabled][checked] {
width:0px; height:0px;
}
input[type='checkbox'][disabled][checked]:after {
content:'\e013'; position:absolute;
margin-top:-10px;
opacity: 1 !important;
margin-left:-5px;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: normal;
}
这也被IE支持:
HTML
class="disabled"
CSS
.disabled{
...
}
filter: invert(25%);
来源。 - Christian Long