最近项目中遇到需求:只在第一列不能删除,不显示小叉号;点击可添加一列,后面的列右上角显示小叉号,能够点击删除。html
我是使用如下方法解决这个小需求ide
:CSS伪类选择器:first-child设置全部小叉号不显示,当点击添加一列时,用jQuery过滤选择器只控制第一个不显示小叉号url
.rule-delete { position: absolute; right: 16px; top: 11px; width: 20px; height: 20px; background: url("../../homepage/images/btn_ic_cancle.png") no-repeat; background-size: cover; } .rule-delete:first-child { display: none; }
//或者
.rule-delete:nth-child(1) { display: none; }
$(".rule-delete").show(); $(".rule-delete:first").hide();//第一个策略没有删除叉号
在解决的过程当中,我还踩了了个坑,误用:frist-child。为了不之后继续踩坑,如今用个小例子记录下jQuery过滤选择器:first和:first-child的区别。spa
:first过滤器只匹配第一个子元素,而:frist-child过滤器将为每一个父元素匹配一个子元素。code
对于下面的html代码:htm
<ul> <li>John</li> <li>Karl</li> <li>Brandon</li> </ul> <ul> <li>Glen</li> <li>Tane</li> <li>Ralph</li> </ul>
$("ul li:first").text();获得的结果为John.
$("ul li:first-child").text();获得的结果为John和Glen.blog