April 24, 2012

last-child() and :after not working in IE 8

Question by Kamal

Hello friends following is my code

HTML

<ul class="menu-item">
    <li>item</li>
    <li>item</li>
    <li>item</li>
</ul>

CSS

.clear-right{
color:red;
}
.menu-item:last-child{
    color:red;}
.menu-item:last-child:after
    {
content:'hi';
}

my code is working perfectly in all browser but not working in IE m trying to do same thng using jquery

$(".menu-item li:last-child:after").css('content','"hi"');

you can check demo here
but its also not working please help me

Thanks in advance

Answer by Starx

:after and :before are pseudo elements, which cannot be manipulated afterwards. So you can’t do this.

An alternate possible selector would be this

$(".menu-item li").last().css('content', 'hi');

One trick to solve it is.

Create a class with your needed CSS

.newClass {}
.newClass:after { content: 'hi'; }

Now using jQuery switch them.

$(".menu-item li").last().addClass("newClass");

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!