:first-child 在上面动态插入不同类的元素时失败

发布于 2024-09-05 23:57:53 字数 1159 浏览 7 评论 0原文

所以,我遇到过这样的情况:插入不同类/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意犹 2024-09-12 23:57:53

这是因为第一个具有 nSub 类的元素不再是父元素的 first-child,因此样式不再匹配。

如果动态插入的元素也具有类 nSub,则规则仍然会匹配,并且会匹配新插入的元素(现在是第一个子元素)。

我不是 CSS3 专家,但你可以尝试 :nth-of-type 选择器:

.nSub:nth-of-type(1) {
   /* Rules for the first .nSub here */
}

That's because the first element with class nSub is no longer the first-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:

.nSub:nth-of-type(1) {
   /* Rules for the first .nSub here */
}
谁许谁一生繁华 2024-09-12 23:57:53

这是因为,我猜你没有为这个插入的元素设置类......

在你的CSS文件中你说“.nSub:first-child”,但你插入的元素不属于该类:“

如果您也将类属性添加到该元素,也许会有所帮助:

$('nav').insert({top:'<div id="newWF" class="nSub"></div>'});

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:

$('nav').insert({top:'<div id="newWF" class="nSub"></div>'});
烂柯人 2024-09-12 23:57:53

PatrikAkerstrand 关于规则不再匹配的说法是正确的。 :first-child 伪(不幸的是)仅针对其父级的第一个子级,该子级也具有元素/类/您指定的任何内容。

我只是花了半个小时咒骂为什么下面的方法不起作用

<div id="header">
  <a href="."><img src="path/file.png" /></a>
  <div class="img"></div>
  <div class="img"></div>
  <div class="img"></div>
</div>

#header .img {
  margin:0 25px;
}
#header .img:first-child,
#header .img:last-child {
  margin:0;
}

我发现解决方案是将 div.img 包装到 div.images 中,就像这样

<div id="header">
  <a href="."><img src="path/file.png" /></a>
  <div class="images">
    <div class="img"></div>
    <div class="img"></div>
    <div class="img"></div>
  </div>
</div>

#header .images .img {
  margin:0 25px;
}
#header .images .img:first-child,
#header .images .img:last-child {
  margin:0;
}

编辑:如果你不想要添加非语义解决方法标记,您可以使用 :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

<div id="header">
  <a href="."><img src="path/file.png" /></a>
  <div class="img"></div>
  <div class="img"></div>
  <div class="img"></div>
</div>

#header .img {
  margin:0 25px;
}
#header .img:first-child,
#header .img:last-child {
  margin:0;
}

I found that the solution were to wrap the div.img's into a div.images, like this

<div id="header">
  <a href="."><img src="path/file.png" /></a>
  <div class="images">
    <div class="img"></div>
    <div class="img"></div>
    <div class="img"></div>
  </div>
</div>

#header .images .img {
  margin:0 25px;
}
#header .images .img:first-child,
#header .images .img:last-child {
  margin:0;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文