:disabled 伪类选择和样式化被禁用的元素。
这些元素通常是表单元素,例如按钮(<button>)、选择菜单(<select>)、输入类型(<input>)和文本区域(<textarea>)。
被禁用的元素不接受点击、文本输入或者焦点。
语法
:disabled {
css declarations;
}
为禁用的 <input> 元素设置背景颜色的示例:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
input {
padding: 2px 5px;
margin-bottom: 10px;
border: 1px solid #ccc;
}
input[type=text]:enabled {
background: #eee;
}
input[type=text]:disabled {
background: #ccc;
}
</style>
</head>
<body>
<h2>:disabled selector example</h2>
<form action="">
<label for="name">First name:</label>
<input type="text" value="John" id="name">
<br>
<label for="lastname">Last name:</label>
<input type="text" value="Smith" id="lastname">
<br>
<label for="country">Country:</label>
<input type="text" disabled="disabled" value="10 High Street" id="conutry">
</form>
</body>
</html>
为禁用的 <option> 元素设置背景颜色的示例:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
option:disabled {
background: #8ebf42;
}
</style>
</head>
<body>
<h2>:disabled selector example</h2>
<select>
<option value="paris">Paris</option>
<option value="london" disabled>London</option>
<option value="moscow">Moscow</option>
<option value="rome" disabled>Rome</option>
<option value="berlin">Berlin</option>
</select>
</body>
</html>
禁用 <input> 元素的示例:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
input {
width: 60%;
margin: 0;
border: none;
outline: 1px solid lightgrey;
outline-offset: 2px;
}
input:disabled {
background: #cccccc;
cursor: not-allowed;
}
form {
background: #67a6ec;
padding: 1.5em;
max-width: 400px;
width: 100%;
outline: 10px solid rgba(17, 58, 103, 0.6);
}
hr {
visibility: hidden;
}
label {
margin-right: 3%;
text-align: left;
display: inline-block;
width: 35%;
}
</style>
</head>
<body>
<h2>:disabled selector example</h2>
<form action="#">
<label for="name">Enabled Input:</label>
<input type="text" autofocus>
<hr>
<label for="name">Disabled Input:</label>
<input type="text" disabled>
</form>
</body>
</html>
日期:2020-06-02 22:14:28 来源:oir作者:oir
