处理JQuery:最后一个选择器

时间:2011-05-14 15:04:24

标签: jquery visible

我正在尝试检测这是否是列表的最后一个元素。这段代码不起作用。会怎么样?

if((visel).is($('#twitnews li:last'))) {
        var nextel =  $('#twitnews li').first();
    }
    else {
    var nextel = $(visel).next();
    }

HTML :(动态生成,但这里是一页的来源)

<div id="twitnews" style="padding-left:20px; line-height:20px; float:left;">

        <li>
         Rawwwwrrrr! (@ Great Life Golf & Fitness- Berkshire) http://4sq.com/kCoZKV
        </li>
        <li>
         Swinley Forest Golf Club, Coronation Road, Ascot, Berkshire - Golf Courses http://t.co/fuoN2LW via @AddThis
        </li>
        <li>
         Had to resort to biking indoors tonight. (@ Great Life Golf & Fitness- Berkshire) http://4sq.com/jhbqK2
        </li>
        <li>
         In my golf course review of The Berkshire Blue course I found this to be a great day out, the course is fantastic... http://fb.me/AYkQDseI
        </li>
        <li>
         I'm at Berkshire Hills Golf Club http://4sq.com/kUAPK2
        </li>
        <li>
         Played Berkshire Valley GC in Morris County NJ.  Course was really nice.  Pics and video review of the course.  http://bit.ly/mAjRcz
        </li>
        <li>
         Review for: The Berkshire Golf Club Blue Course. Great course http://bit.ly/hUqV8e
        </li>
        <li>
         A group of four friends from West Berkshire are getting ready to tee off for a charity golf challenge http://bit.ly/l1IWD6
        </li>
        <li>
         I'm at Great Life Golf & Fitness- Berkshire (3720 SW 45th St., at Stone Ave., Topeka) http://4sq.com/jbOX6g
        </li>
        <li>
         Berkshire golf club, hang your head in shame with the state those greens were in! Worst greens i have putted on EVER! #horrific
        </li></div>
<script type="text/javascript">
$('#twitnews li').hide();
$('#twitnews li').first().fadeIn(300, function fade(){

    var visel = $('#twitnews li:visible');

    if($(visel).is($('#twitnews li:last'))) {
        var nextel =  $('#twitnews li').first();
    }
    else {
    var nextel = $(visel).next();
    }


    $(visel).delay(3000).fadeOut(300, function(){

        $(nextel).fadeIn(300, function(){fade()});

        });

    });
        </script>

2 个答案:

答案 0 :(得分:2)

如果您使用的是jquery 1.6:

,则可以正常工作

http://jsfiddle.net/Da8ja/2/

如果你低于1.6,那么你需要改变这个:

visel.is($('#twitnews li:last'))

1.6中添加了将jquery对象传递给is的功能。你需要做这样的事情:

if(visel[0]==$('#twitnews li:last')[0]) {

http://jsfiddle.net/Da8ja/3/

答案 1 :(得分:-1)

您错过了$

if($(visel).is($('#twitnews li:last')))