nth-of-type和nth-child的区别
看CSS3时发现了一个nth-of-type选择器,发现平时基本没见过用,就研究了一下,w3c是这样说明的:
:nth-of-type(n) 选择器匹配属于父元素的特定类型的第 N 个子元素的每个元素.
看起来和nth-child很像
:nth-child(n) 选择器匹配属于其父元素的第 N 个子元素,不论元素的类型。
那么两者区别到底是什么?试验一下吧。
HTML:
<div class="box">
box:
<p>我是p1</p>
<p>我是p2</p>
</div>
<div class="pox">
pox:
<p>我是p1</p>
<p>我是p2</p>
</div>
CSS:
<style>
.box{
margin: 100px;
float: left;
}
.pox{
margin: 100px 0 0 60px;
float: left;
}
.pox p:nth-child(1){
color: red;
}
.box p:nth-of-type(1){
color: red;
}
</style>
结果貌似相同:

这里稍微做一下改变HTML:
<div class="box">
box:
<div>我是div1</div>
<p>我是p1</p>
<div>我是div2</div>
<p>我是p2</p>
</div>
<div class="pox">
pox:
<div>我是div</div>
<p>我是p1</p>
<div>我是div2</div>
<p>我是p2</p>
</div>
结果:

这里发现nth-child没有起作用,这是为什么呢?
其实p:nth-of-type(n)是指父元素下第n个p元素, 而p:nth-child(n)是指父元素下第n个元素且这个元素为p,若不是,则选择失败。
这里的pox下的第一个子元素是div而不是p,所以选择失败。若想p1变红,p1是pox下的第二个子元素应该选择nth-child(2),应该改为:
.pox p:nth-child(2){ color: red; }
结果:

nth-of-type和nth-child的区别的更多相关文章
- 网站开发进阶(四十四)input type="submit" 和"button"的区别
网站开发进阶(四十四)input type="submit" 和"button"的区别 在一个页面上画一个按钮,有四种办法: 这就是一个按钮.如果你不写ja ...
- <input type="image"> 和 <img> 用法区别
原文:<input type="image"> 和 <img> 用法区别 w3c定义如下: Image <input type="image ...
- <input type="text">和<textarea>的区别
在我们开发时经常需要用到输入框,通常解决办法就是<input type="text">和<textarea>,那么这两个标签有什么区别呢? 一:<i ...
- go type别名和定义类型区别
package main import ( "fmt" ) type person struct { age int name string } func (p person)te ...
- input[type="button"]与<button>的区别
<button>标签 浏览器支持 所有主流浏览器都支持<button>标签. 重要事项:如果在HTML表单中使用button元素,不同的浏览器会提交不同的值.IE将提交& ...
- qtp type和set方法的区别
type模拟键盘输入,是一个字符一个字符的输入. set是整个输入框一起置值. 例如:winedit中输入a后,再用type输入b,结果就是ab 如果用set输入a后,再用set输入b,结果就是b. ...
- type='button'和'submit'的区别
今天在对表单的项目进行删除时出现了问题,原因就出现在点击input按钮时,这个input属性是type='button'还是type='submit'. 代码大致如下: <script type ...
- instance of type of object.prototype.tostring 区别
typeof typeof 是一个操作符,其右侧跟一个一元表达式,并返回这个表达式的数据类型. 返回的结果用该类型的字符串(全小写字母)形式表示,包括以下 6 种: number.boolea ...
- isinstance 和 type 的区别
class A: pass class B(A): pass isinstance(A(), A) # returns True type(A()) == A # returns True isins ...
- c# 之 System.Type.GetType()与Object.GetType()与typeof比较
Object.GetType()与typeof的区别 //运算符,获得某一类型的 System.Type 对象. Type t = typeof(int); //方法,获取当前实例的类型. ; Con ...
随机推荐
- StateMachine
Create State Machine Create either a passive or an active state machine: ? 1 var fsm = new PassiveSt ...
- SIT和UAT的区别
SIT和UAT有什么区别?谢谢! 系统内部集成测试(System Integration Testing) SIT 用户验收测试(User Acceptance Testing) UAT ...
- struts2标签获取parameter,request,session,application中的值
http://localhost:8080/demo/index.jsp?flag=kkkk <s:property value="#parameters.flag" /&g ...
- 关于DevExpress的GridView.VisibleIndex的赋值问题
在DevExpress GridControl中,GridView中 如果VisibleIndex=-1,则这列将不会显示(不可见): 如果VisibleIndex>=0,则按照VisibleI ...
- DIV+CSS滑动门效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C++学习13 类class和结构体struct的区别
C++保留了C语言的 struct,并且加以扩充.在C语言中,struct 只能包含数据成员,不能包含成员函数.而在C++中,struct 类似于 class,既可以包含数据成员,又可以包含成员函数. ...
- 在delphi下TClientSocket的使用技巧 转
http://blog.csdn.net/newzhhsh/article/details/2905874 如果你是在线程的构造函数中创建TClientSocket,那么TClientSocket还是 ...
- (easy)LeetCode 228.Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- oracle分布式事务总结-转载
基本概念 Local Coordinator:在分布事务中,必须参考其它节点上的数据才能完成自己这部分操作的站点. Global Coordinator:分布事务的发起者,负责协调这个分布事务. Co ...
- JQuery基础教程:事件(上)
在页面加载后执行任务 之前我们已经知道了$(document).ready()是jQuery基于页面加载执行任务的一种主要方式,但是要知道原生的window.onload事件也可以实现相同的 ...