Sibling + selector does't work in IE7 when using jQuery

前端 未结 3 1904
后悔当初
后悔当初 2021-01-20 09:08

Trying to select a sibling element using jQuery and it doesn\'t work in IE7.

Here is my sample code: http://jsfiddle.net/y7AHz/6/ (click Run to see result)

相关标签:
3条回答
  • 2021-01-20 09:35

    This is a known bug with sizzle (the selector engine underneath jQuery) in IE6 and 7. Instead, use .next(), which is equivalent to the + or next adjacent selector, and .find(). It's more verbose, but it works in IE6+:

    var numberOfListItems = $("#txtInput").next(".ulContainer").find("li").length;
    

    Updated jsFiddle

    0 讨论(0)
  • 2021-01-20 09:38

    try only this it works in ie7 also

    var numberOfListItems = $("ul.ulContainer li").length;
    
    0 讨论(0)
  • 2021-01-20 09:58

    This will also work in IE7:

    var foo= $("#txtInput + ul.ulContainer");
    var numberOfListItems = $('li', foo).length;
    
     $("#output").text(numberOfListItems);
    

    Not sure which of the proposed fixes performs best, if it matters...

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