:first-of-type
选择所有相同的元素名称的第一个兄弟元素。
jQuery(":first-of-type")
:first-of-type
选择器匹配元素,在文档树中,相同的父元素并且在其他相同的元素名称之前。
例子
在每一个匹配的div中查找第一个span,给它添加一个类。
<!DOCTYPE html> <html> <head> <style> span.fot { color: red; font-size: 120%; font-style: italic; } </style> <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.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>