li:last-child doesn't seem to work in IE8

2019-02-24 22:48发布

问题:

Below is my html structure

<div class="footerMenu">
   <ul>
     <li>Home</li>
     <li>About</li>
     <li>Feedback</li>
     <li>Contact us</li>        
   </ul>            
</div>

But

.footerMenu li:last-child { } 

selector doesn't seem to work in IE8. But http://msdn.microsoft.com/en-us/library/cc351024%28VS.85%29.aspx tells that the pseudo-selector is suppported.Any help on this!

回答1:

You read it wrong. It says that it's not supported in IE8:

If you were looking at :first-child, which does have support in IE7 and IE8, and thinking that the same applies to :last-child... surprise! It doesn't.

:first-child is a CSS2 selector, but :last-child was only introduced in CSS3, so since Microsoft was only aiming for CSS2.1 compliance with IE8, they likely didn't bother about :last-child until post-IE8.

If you know you will only have four li elements, then you should be able to use adjacent sibling selectors to reach the fourth li:

.footerMenu li:first-child + li + li + li


回答2:

To build on the other guys answers, an alternative could be to use javascript to fill the gaps, selectivizr is a good example of adding last-child support.

http://selectivizr.com/



回答3:

The link you provided shows that it isn't supported for IE8... IE9+ only. Googling last-child IE8 brings up a whole host of similar queries.



回答4:

Where did you read that? :first-child is supported back to IE7, but :last-child is IE9 and later.

(Headers moved down for your convenience)



回答5:

The other answers are correct that IE8 doesn't support last-child. However, to solve your particular problem, you could either a) manually add a class to the last <li> or b) as this is a menu, and these will presumably have links inside them, target the last link with an attribute selector, which does work in IE8. Something like

.footermenu a[href="contact.html"] { ... }


回答6:

Try to use something like that:

.footerMenu li {background-color: expression(this.previousSibling==null?'red':'green');}


回答7:

I know its old but...there is a simple way to do it:

Just in the li you want the change do this:

<li style="yourstyle;">...</li>