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 ...
随机推荐
- iOS 热点、通话时候TabView的Frame调整
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame{ ...
- Vim 中截取部分内容保存到其他文件
最近无聊,突然想跟着玩玩天池数据挖掘,发现数据好大,想转换到mysql数据库,phpmyadmin import 导入时抱错! 数据文件大大! 于是乎,准备截取一小段到另外一个文件测试先,然后,发现了 ...
- [ActionScript 3.0] AS3.0 获取像素点的灰度
/** * 获取像素点的灰度 * @color 像素点的颜色值 * @return uint */ function getGray(color:uint):uint { return getR(co ...
- [ActionScript 3.0] AS3 访问舞台上元件的方法
文档类: package { import flash.display.MovieClip; public class Main extends MovieClip { public function ...
- sopcinfo路径改变,nios工程该怎么办?
操作系统:Win7 64 bit 开发环境:Quartus II 14.0 (64-Bit) + Nios II EDS 14.0 使用Quartus 时,有时候出于备份的考虑,或者从网上下载别人的 ...
- gnu c语言中的?:的作用
#include <stdio.h> #include <stdlib.h> char * test() { return "abc" ?: "f ...
- (转)由Uploadify插件想到的Flash无法传递Session和Cookie的问题解决
在ASP.NET MVC3中使用Uploadify上传文件时发现,在后台需要验证登录状态的时候,Uploadify根本无法完成验证,因此,在后台只能使用非验证状态进行文件上传 —— 众所周知,这无异于 ...
- 菜鸟-手把手教你把Acegi应用到实际项目中(11)-切换用户
在某些应用场合中,我们可能需要用到切换用户的功能,从而以另一用户的身份进行相关操作.这一点类似于在Linux系统中,用su命令切换到另一用户进行相关操作. 既然实际应用中有这种场合,那么我们 ...
- 为view添加约束constraints
在相应要设置约束的view中按住鼠标右键进行拖拽,然后向指定的方向添加约束,如图: 拖拽的时候会显示一条蓝线,如上图所示,然后手指离开鼠标的时候会弹出向对应的约束供添加约束的时候进行使用如图:
- Codeforces Round #218 (Div. 2) D. Vessels
D. Vessels time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...