日期:2014-05-17 浏览次数:20753 次
UI元素状态伪类选择器的共同特征是:指定的样式只有当元素处于某种状态下时才起作用,在默认状态下不起作用。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>E:hover, E:active and E:focus</title>
<style type="text/css">
input[type="text"]:hover {background-color: greenyellow;}
input[type="text"]:focus {background-color: skyblue;}
input[type="text"]:active {background-color: yellow;}
</style>
</head>
<body>
<form>
<label>姓名:</label><input type="text" name="name"/><br/>
<label>地址:</label><input type="text" name="addr"/>
</form>
</body>
</html>
当一个表单中的元素经常在可用状态与不可用状态之间进行切换的时候,通常会将这两个选择器结合起来使用,使用示例如下:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8"/>
<title>E:enabled, E:disabled</title>
<style type="text/css">
input[type="text"]:enabled {background-color: yellow;}
input[type="text"]:disabled {background-color: purple;}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
$("input[name=ena]").change(function(event) {
var element = event.target;
if(element.checked && element.value == "true") {
$("input[name=tx]").removeAttr("disabled");
} else {
$("input[name=tx]").attr("disabled", "disabled");
}
});
});
</script>
</head>
<body>
<form>
<fieldset>
<label><input type="radio" name="ena" value="true" checked="checked"/>可用</label>
<label><input type="radio" name="ena" value="false"/>不可用</label>
</fieldset><br/>
<input type="text" name="tx"/>
</form>
</body>
</html>
注意:在Firefox浏览器中,需要写成“-moz-read-only”和“-moz-read-write”的形式。
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8"/>
<title>E:read-only, E:read-write</title>
<style type="text/css">
input[type="text"]:-moz-read-only {background-color: gray;}
input[type="text"]:-moz-read-write {background-color: greenyellow;}
input[type="text"]:read-only {background-color: gray;}
input[type="text"]:read-write {background-color: greenyellow;}
</style>
</head>
<body>
<form>
<label>名字:</label><input type="text" name="name"/><br/>
<label>地址:</label><input type="text" name="addr" value="江苏省常州市" readonly="readonly"/>
</form>
</body>
</html>
该选择器用来指定当元素处于先中状态时的样式。注意:在Firefox浏览器中需要写成“-moz-selection”的形式。 使用示例如下:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8"/>
<title>E:enabled, E:disa