:only-child
如果某个元素是其父元素的唯一子元素,那么它就会被选中。
-
1.1.4 新增:only-child
如果某个父元素还有其它的子元素,那么它将不会被匹配到。
示例:
Change the text and add a border for each button that is the only child of its parent. 若每个 div 中只有一个按钮,则改变这些按钮的外观。
<!DOCTYPE html>
<html>
<head>
<style>
div { width:100px; height:80px; margin:5px; float:left; background:#b9e }
</style>
<script src="jquery.min.js"></script>
</head>
<body>
<div>
<button>Sibling!</button>
<button>Sibling!</button>
</div>
<div>
<button>Sibling!</button>
</div>
<div>
None
</div>
<div>
<button>Sibling!</button>
<button>Sibling!</button>
<button>Sibling!</button>
</div>
<div>
<button>Sibling!</button>
</div>
<script>
$("div button:only-child").text("Alone").css("border", "2px blue solid");
</script>
</body>
</html>