:first-of-type 选择器| jqueryAPI 2.2 中文手册- AspRain.cn 致力于Web开发技术翻译整理
From jQuery API 2.2.0
:first-of-type 选择器
first-of-type selector
描述:选择这样的元素:它们属于它们的同名的同辈元素中的第一个。
加入于: 1.9
jQuery( ":first-of-type" ):first-of-type选择器匹配没有在document树中没有和它父元素相同、标签名相同的元素在它前面的元素。
示例
在每个匹配的<div>中找到第一个<span>并给它加一个样式类。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>first-of-type demo</title>
<style>
span.fot {
color: red;
font-size: 120%;
font-style: italic;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div>
<span>Corey,</span>
<span>Yehuda,</span>
<span>Adam,</span>
<span>Todd</span>
</div>
<div>
<b>Nobody,</b>
<span>Jörn,</span>
<span>Scott,</span>
<span>Timo</span>
</div>
<script>
$( "span:first-of-type" ).addClass( "fot" );
</script>
</body>
</html>
演示结果
如果网页上不能运行示例,请点击http://www.asprain.cn/jQueryAPI/first-of-type-selector.htm查看示例。
如果你觉得本文档对你有用,欢迎给翻译作者支付宝打赏,支持翻译作者源源不断翻译更多有用的技术文档。