nth-child doesn't work in IE7/IE8

后端 未结 3 1516
Happy的楠姐
Happy的楠姐 2021-01-18 08:48

I cannot get the :nth-child selector to work with IE7/8.

Here is a working example of my code (that works in Chrome)

Below is the CSS and HTML I

相关标签:
3条回答
  • 2021-01-18 09:25

    Below example may be helpful for you

    //For first child
    // equivalent to li:nth-child(1)
    li:first-child a {
        border-top: 5px solid red;
    }
    
    //For second child
    // equivalent to li:nth-child(2)
    li:first-child + li a {
        border-top: 5px solid blue;
    }
    
    //For third child
    // equivalent to li:nth-child(3)
     li:first-child + li + li a {
        border-top: 5px solid green;
    }​
    
    0 讨论(0)
  • 2021-01-18 09:31

    That's because :nth-child isn't supported in IE7/IE8.

    One solution to this problem would be to use Selectivizr.

    "Selectivizr is a JavaScript utility that emulates CSS3 pseudo-classes and attribute selectors in Internet Explorer 6-8."

    All you need to do is include the Selectivizr script and, if you aren't already using one, decide which JavaScript library you'd like to use (jQuery, Mootools etc.) and you will have support for the :nth-child selector (amongst various other pseudo-selectors/attribute selectors) in IE6 through to IE8.

    Edit:

    In reply to your comment, here's a quick tutorial showing you how to set up and use Selectivizr.

    0 讨论(0)
  • 2021-01-18 09:37

    Use a polyfill such as selectivizer for the missing functionality.

    1. Download it from selectivizr.com at http://selectivizr.com/downloads/selectivizr-1.0.2.zip
    2. Unzip it and put the file in in your project under app/assets/javascripts
    3. Reference it in your app for ie only with <!--[if (gte IE 6)&(lte IE 8)]><script type="text/javascript" src="selectivizr-min.js"></script><![endif]--> in your layout's application.js file. or...
    4. For the asset pipeline you can add gem 'selectivizr-rails' to our Gemfile and then bundle install. You get the gem from https://github.com/jhubert/selectivizr-rails

      Then add the following to the head tag in your layout:

      <!--[if (gte IE 6)&(lte IE 8)]> = javascript_include_tag 'selectivizr' <![endif]-->

    5. proceed as normal

    0 讨论(0)
提交回复
热议问题