js进阶 10-9 -of-type型子元素伪类选择器

一、总结

一句话总结:三种和first、last等有关的选择器。

1、:first和:first-child和:first-of-type的区别?

:first:单选。:first-child:多选(父元素第一个)。:first-of-type:多选(同类型第一个);

:first-of-type”是选择父元素下某个元素类型的第1个子元素(区分元素类型);“:first-child”是选择父元素下的第1个子元素(不区分元素类型)

二、-of-type型子元素伪类选择器

1、相关知识

子元素伪类选择器

    • :first-child选择父元素的第1个子元素
    • :last-child选择父元素的最后1个子元素
    • :nth-child(n)选择父元素下的第n个元素或奇偶元素,n的值为"整数|odd|vevn
    • :only-child选择父元素中唯一的子元素(该父元素只有一个子元素)
    • :first-of-type选择同元素类型的第1个同级兄弟元素
    • :last-of-type选择同元素类型的最后1个同级兄弟元素
    • :nth-of-type选择同元素类型的第n个同级兄弟元素,n的值可以是"整数|odd|even"
    • :onlt-of-type匹配父元素中特定类型的唯一子元素(但是父元素可以有多个子元素)

2、代码

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>演示文档</title>
<script src="jquery-3.1.1.min.js"></script>
<style type="text/css">
</style>
</head>
<body>
<div>
<h1>h1</h1>
<p>p1</p>
<p>p2</p>
<span>span1</span>
<span>span2</span>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
</ul>
</div>
<script>
// $('li:nth-child(3n)').css("background-color", "red")
// $('h1:first-child').css("background-color", "red") //选择择不到任何元素,p、span并不是div的第1个子元素
// $('p:first-child').css("background-color", "blue")
// $('span:first-child').css("background-color", "blue") //“:first-of-type”是选择父元素下某个元素类型的第1个子元素(区分元素类型);“:first-child”是选择父元素下的第1个子元素(不区分元素类型)
$('h1:first-of-type').css("background-color", "red")
$('p:first-of-type').css("background-color", "red")
$('span:first-of-type').css("background-color", "red")
</script>
</body>
</html>
 
 

最新文章

  1. web项目 log4j2的路径问题
  2. Python::re 模块 -- 在Python中使用正则表达式
  3. 开发板支持wifi
  4. Windows下安装Ruby
  5. 支持.NET和移动设备的XLS读写控件XLSReadWriteII下载地址及介绍
  6. 【转】#ifdef _DEBUG用法小结
  7. jmeter if 控制器
  8. KMP入门题目[不定期更新]
  9. 基于单例使用ThreadLocal对多线程下数据的访问修改
  10. 用Express搭建 blog (一)
  11. CreateCompatibleBitmap 需要注意的问题
  12. 远程桌面协议浅析(VNC/SPICE/RDP)
  13. 操作json数据
  14. Cookie和Session的原理和异同
  15. Oracle 中 编写 function 和 procedure 的注意事项
  16. python大法好——Python2.x与3​​.x版本区别
  17. centos 6 中恢复删除的文件
  18. synchronized和Lock的异同
  19. PHP并发操作下的加锁
  20. Servlet 思维导图

热门文章

  1. COGS——T 2057. [ZLXOI2015]殉国
  2. php线性表数组实现的删除操作
  3. python内存增长问题
  4. ActiveX控件开发 C#
  5. 2016最热门的PHP框架(一共五款)
  6. Android检测网络状态,判断当前网络是否可用
  7. Keepalived + Mysql 双主
  8. Eclipse中开发环境也想把Tomcat 的默认BIO模式改为NIO模式
  9. [Angular] Setup automated deployment with Angular, Travis and Firebase
  10. ios sqlite数据库操作