:first-child 在上面动态插入不同类的元素时失败
所以,我遇到过这样的情况:插入不同类/id 的元素会破坏该 :first-child 上的所有 css 规则。
<div id="nav">
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
</div>
.nSub:first-child { margin-top:15px; -moz-border-radius-topleft:5px; /* ... */ }
.nSub { background:#666; /* ... */ }
.nSub:last-child { -moz-border-radius-bottomleft:5px; /* ... */ }
一旦我插入上面另一个类/id 的元素,如下所示:
$('nav').insert({top:'<div id="newWF"></div>'});
.nSub:first-child 的所有声明在 FF 3.6 和 Safari 4 中都将被忽略。
编辑: 抱歉,如果我没有说清楚:上面插入的元素应该没有类名“.nSub”
<div id="nav">
<div id="newWF"></div>
<div class="nSub">abcdef</div> <!-- BROKEN CSS -->
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
</div>
So, I've encountered a situation where inserting an element of a different class/id breaks all css-rules on that :first-child.
<div id="nav">
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
</div>
.nSub:first-child { margin-top:15px; -moz-border-radius-topleft:5px; /* ... */ }
.nSub { background:#666; /* ... */ }
.nSub:last-child { -moz-border-radius-bottomleft:5px; /* ... */ }
As soon as I insert an element of another class/id above, like this:
$('nav').insert({top:'<div id="newWF"></div>'});
all declarations for .nSub:first-child are being ignored in both FF 3.6 and Safari 4.
EDIT:
sorry if I did not say it clearly: the element inserted above is supposed to NOT have the classname ".nSub"
<div id="nav">
<div id="newWF"></div>
<div class="nSub">abcdef</div> <!-- BROKEN CSS -->
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
<div class="nSub">abcdef</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为第一个具有
nSub
类的元素不再是父元素的first-child
,因此样式不再匹配。如果动态插入的元素也具有类
nSub
,则规则仍然会匹配,并且会匹配新插入的元素(现在是第一个子元素)。我不是 CSS3 专家,但你可以尝试
:nth-of-type
选择器:That's because the first element with class
nSub
is no longer thefirst-child
of the parent, and thus the style no longer matches.If the dynamically inserted element would also have class
nSub
, then the rule would still match, and match for the newly inserted element (which is now the first child).I'm no CSS3 expert, but you could try the
:nth-of-type
selector:这是因为,我猜你没有为这个插入的元素设置类......
在你的CSS文件中你说“.nSub:first-child”,但你插入的元素不属于该类:“
如果您也将类属性添加到该元素,也许会有所帮助:
This is because, you don't set the class for this inserted element, I guess...
In you CSS-File you say ".nSub:first-child", but the element you are inserting is not of that class: "
Maybe it helps, if you add the class-attribute to that element, too:
PatrikAkerstrand 关于规则不再匹配的说法是正确的。 :first-child 伪(不幸的是)仅针对其父级的第一个子级,该子级也具有元素/类/您指定的任何内容。
我只是花了半个小时咒骂为什么下面的方法不起作用
我发现解决方案是将 div.img 包装到 div.images 中,就像这样
编辑:如果你不想要添加非语义解决方法标记,您可以使用 :first-of-type 伪类。但是,早期版本的 IE 不支持此功能。
What PatrikAkerstrand said about the rule no longer matching is correct. The :first-child pseudo (unfortunately) only targets the first child of its parent that also has the element/class/whatever you specified.
I just spent half an hour cursing at why the following wouldn't work
I found that the solution were to wrap the div.img's into a div.images, like this
Edit: If you don't want to add non-semantic workaround markup, you can use the :first-of-type pseudo-class. This, however, is not supported in earlier versions of IE.