小白教程
所有教程
关于
Search
172.70.178.109
172.70.178.109
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Sel firstoftype”的源代码
本页内容
上一节:
Jq_sel_firstchild
下一节:
Sel_lastchild
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE::first-of-type}}[[Category:jquery ref selectors|12]] = jQuery :first-of-type 选择器 = [[jquery ref selectors|jQuery 选择器]] 选取属于其父元素的第一个 <p> 元素的每个 <p> 元素: <sample title="" desc="" lang="html" hererun="1"> $("p:first-of-type") </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("p:first-of-type").css("background-color","yellow"); }); </script> </head> <body> <p>body 中第一个段落。</p> <div style="border:1px solid;"> <p>div 中第一个段落。</p> <p>div 中的最后一个段落。</p> </div><br> <div style="border:1px solid;"> <span>这是一个 span 元素。</span> <p>另一个 div 中第一个段落。</p> <p>另一个 div 中的最后一个段落。</p> </div> <p>body 中最后一个段落。</p> </body> </html> </run> == 定义和用法 == :first-of-type 选择器选取属于其父元素的特定类型的第一个子元素的所有元素。 '''提示:'''该选择器与 :nth-of-type(1) 相同。 '''提示:'''请使用 [[sel lastoftype|:last-of-type]] 选择器来选取属于其父元素的特定类型的最后一个子元素的所有元素。 == 语法 == $(":first-of-type") == 尝试一下 - 实例 == [[try.php?filename=tryjquery_sel_firstoftype2|选取所有 <div> 元素的第一个 <p> 元素]] 如何选取所有 <div> 元素的第一个 <p> 元素。 [[try.php?filename=tryjquery_sel_firstoftypediff|:first、:first-child 和 :first-of-type 之间的不同]] 演示 :first、:first-child 和 :first-of-type 选择器之间的不同。 [[jquery ref selectors|jQuery 选择器]]
返回至“
Sel firstoftype
”。
上一节:
Jq_sel_firstchild
下一节:
Sel_lastchild