: nth-child не работает в IE - PullRequest
       0

: nth-child не работает в IE

2 голосов
/ 17 октября 2010

Я искал свою конкретную проблему и не могу ее найти ... Я надеюсь, что любой из вас, ребята, сможет помочь.

Я пытаюсь заставить n-го ребенка работать в IE - теперь я прочитал, что выможно сделать это с помощью Jquery, как мне реализовать jquery в этом случае?

Я также использую этот элемент Lib ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js

Он отлично работает в Firefox, но не в IE - пожалуйста, помогите - спасибо

        <div class="info-col">
        <h2>Header 1</h2>
        <a class="imagehref="imagepath">View Image</a>

        <dl>
          <dt>Sub header 1</dt>
             <dd>Copy 1.</dd>
          <dt>Sub header 2</dt>
             <dd>Copy2</dd>
          <dt>Sub header 3</dt>
             <dd>Copy 3</dd>
          <dt>Sub header 4</dt>
             <dd>Copy 4</dd>
          <dt>Sub header 5</dt>
             <dd>Copy 5</dd>
          <dt>Sub header 6</dt>
             <dd>Copy 6</dd>
        </dl>        
    </div>

Javascript code

$(function() {

// Set up variables
var $el, $parentWrap, $otherWrap, 
    $allTitles = $("dt").css({
        padding: 5, // setting the padding here prevents a weird situation, where it would start animating at 0 padding instead of 5
        "cursor": "pointer" // make it seem clickable
    }),
    $allCells = $("dd").css({
        position: "relative",
        top: -1,
        left: 0,
        display: "none" // info cells are just kicked off the page with CSS (for accessibility)
    });

// clicking image of inactive column just opens column, doesn't go to link   
$("#page-wrap").delegate("a.image","click", function(e) { 

    if ( !$(this).parent().hasClass("curCol") ) {         
        e.preventDefault(); 
        $(this).next().find('dt:first').click(); 
    } 

});

// clicking on titles does stuff
$("#page-wrap").delegate("dt", "click", function() {

    // cache this, as always, is good form
    $el = $(this);

    // if this is already the active cell, don't do anything
    if (!$el.hasClass("current")) {

        $parentWrap = $el.parent().parent();
        $otherWraps = $(".info-col").not($parentWrap);

        // remove current cell from selection of all cells
        $allTitles = $("dt").not(this);

        // close all info cells
        $allCells.slideUp();

        // return all titles (except current one) to normal size
        $allTitles.animate({
            fontSize: "14px",
            paddingTop: 5,
            paddingRight: 5,
            paddingBottom: 5,
            paddingLeft: 5
        });

        // animate current title to larger size            
        $el.animate({
            "font-size": "20px",
            paddingTop: 10,
            paddingRight: 5,
            paddingBottom: 0,
            paddingLeft: 10
        }).next().slideDown();

        // make the current column the large size
        $parentWrap.animate({
            width: 320
        }).addClass("curCol");

        // make other columns the small size
        $otherWraps.animate({
            width: 140
        }).removeClass("curCol");

        // make sure the correct column is current
        $allTitles.removeClass("current");
        $el.addClass("current");  

    }

});

$("#starter").trigger("click");

});

Ответы [ 2 ]

6 голосов
/ 17 октября 2010

Существуют различные способы использования jQuery с :nth-child псевдоселектор:

$('dt:nth-child(odd)') // gets every odd-numbered dt
$('dt:nth-child(even)') // gets every even-numbered dt
$('dt:nth-child(3n)') // gets every third dt

Отредактировано в ответ на вопрос @ Unihost (в комментариях ниже):

Как мне создать и связать этот файл jquery ... Как и любой обычный файл .js?

Безусловно, единственное, что нужно помнить, это то, что вы, вероятно, используете jQuery для применения css, поэтому я бы предложил использовать его следующим образом:

$('dt:nth-child(odd)').addClass('oddDts');
$('dt:nth-child(even)').addClass('evenDts');

И затем добавить следующее (какдемо) к вашему css:

dt:nth-child(odd), /* for useful 'modern' broswers that understand */
dt.oddDts {        /* referencing the class applied with IE-specific jQuery file */
    background-color: #ffa;
}
dt:nth-child(even), /* for useful 'modern' broswers that understand */
dt.evenDts {        /* referencing the class applied with IE-specific jQuery file */
    background-color: #f00;
}

Я бы настоятельно рекомендовал не пытаться применять все соответствующие стили с помощью jQuery, иначе он очень громоздкий очень быстро.Кроме того, таким образом, вы можете использовать псевдоселекторы nth-child() в своем обычном CSS и включать jQuery только для IE:

<!--[if ie]>
    <script src="path/to/jsFile.js" type="text/javascript"></script>
<![end if]-->

Кстати, есть демонстрация намерений JS Fiddle , если вы хотите посмотреть, как это может работать.

3 голосов
/ 17 октября 2010

IE не поддерживает :nth-child.Используйте jQuery с обычным селектором CSS, например:

$('dl dt:nth-child(2n)') // gets every 2nd dt
...