简体   繁体   English

CSS ie8的第一个孩子和最后一个孩子

[英]css ie8 first-child & last-child

Hello guys i have the following.. 大家好,我有以下内容:

.selected2:first-child{
   background: url(../img/css/first-selected.png) no-repeat !important;
   background-position: center center !important;
   box-shadow: inset 1px 1px 5px 2px rgba(221,221,221,1) !important;
}
.selected2{
   background: url(../img/css/second-selected.png) no-repeat !important;
   background-position: center center !important;
   box-shadow: inset 1px 1px 5px 2px rgba(221,221,221,1) !important;
}
.selected2:last-child{
   background: url(../img/css/third-selected.png) no-repeat !important;
   background-position: center center !important;
   box-shadow: inset 1px 1px 5px 2px rgba(221,221,221,1) !important;
}

it works perfect in ie9, chrome, opera, firefox... but in ie8 i get only the second background on every li element. 它在ie9,chrome,opera,firefox中完美运行...但是在ie8中,每个li元素仅获得第二个背景。

What is the problem? 问题是什么? ie8 does not support first-child? ie8不支持第一个孩子?

You have two issues here: Firstly the :last-child selector, and secondly the RGBA background colours. 这里有两个问题:首先是:last-child选择器,其次是RGBA背景色。

  1. IE8 does not support :last-child . IE8不支持:last-child (It does support :first-child though) (虽然它支持:first-child

    You can see the browser support for these (and all other CSS selectors) at Quirksmode.org . 您可以在Quirksmode.org上看到这些(以及所有其他CSS选择器)的浏览器支持。

    The quickest and easiest way to deal with this if you need IE8 support is simply to add classes to the relevant elements and style them that way instead. 如果需要IE8支持,处理此问题的最快,最简单的方法就是简单地将类添加到相关元素并以这种方式设置它们的样式。 It's the old-school way of doing things, but then IE8 is something of an old-school browser. 这是一种老式的处理方式,但是IE8却是一种老式的浏览器。

    If you really need to use selectors like :last-child , and you really need to support IE8, there are Javascript solutions you could try: 如果您确实需要使用:last-child类的选择器,并且确实需要支持IE8,则可以尝试以下Javascript解决方案:

    • http://selectivizr.com/ -- this is a JS library that adds support for a bunch of CSS selectors to old IE versions. http://selectivizr.com/-这是一个JS库,为旧IE版本增加了对一堆CSS选择器的支持。 It requires you to also use another lib such as jQuery that it uses to do the hard work. 它要求您还使用另一个库(例如jQuery)来完成艰苦的工作。

    • https://code.google.com/p/ie7-js/ -- this is a JS library that tries to patch old IE versions to fix as many of the bugs and quirks as possible and back-fill as many missing features as possible. https://code.google.com/p/ie7-js/-这是一个JS库,它试图修补旧版IE版本,以修复尽可能多的错误和怪癖,并回填尽可能多的缺少的功能可能。 It's pretty wide-ranging in what it does. 它的作用范围很广。 It does include most of the advanced CSS features. 它确实包含大多数高级CSS功能。

    Either of these libraries should do the trick for you to add :last-child support, unless you have users with JS turned off. 这些库中的任何:last-child都可以为您增加:last-child支持,除非您已关闭JS用户。

  2. However as I said, IE8 does support :first-child , so missing selectors isn't the reason for your code not working in this case. 但是,正如我所说,IE8确实支持:first-child ,因此缺少选择器并不是您的代码在这种情况下无法正常工作的原因。 The reason your CSS isn't working for :first-child is because you're using an RGBA colour for the background. CSS不适用于:first-child的原因是因为您使用RGBA颜色作为背景。 IE8 doesn't support RGBA colours. IE8不支持RGBA颜色。

    For this, the only solution available is a JS library called CSS3Pie . 为此,唯一可用的解决方案是一个名为CSS3Pie的JS库。 This adds various CSS3 features to IE6/7/8, including RGBA colour support (albeit to a limited extent; it doesn't do everything). 这会在IE6 / 7/8中添加各种CSS3功能,包括RGBA颜色支持(尽管在一定程度上;它不能做所有事情)。

IE8 and lower don't support those pseudo classes. IE8和更低版本不支持这些伪类。 There is a Javascript tool that makes IE7 and IE8 behave closer to IE9 and adds support for first and last childs. 有一个Javascript工具使IE7和IE8的行为更接近IE9,并增加了对第一个和最后一个孩子的支持。

https://code.google.com/p/ie7-js/ https://code.google.com/p/ie7-js/

In the downloaded file, you will find IE7.js IE8.js and IE9.js just use the IE9.js it includes the other two... 在下载的文件中,您会发现IE7.js,IE8.js和IE9.js仅使用IE9.js,其中包括其他两个...

如前所述,您不能使用:last-child伪类,但是可以使用.selected2 + .selected2等来定位所需的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM