开发者

Dealing with the JQuery :last selector

开发者 https://www.devze.com 2023-03-06 03:01 出处:网络
I am trying to detect whether this is the last element of a list. This code did not work. What would?

I am trying to detect whether this is the last element of a list. This code did not work. What would?

if((visel).is($('#twitnews li:last'))) {
        var nextel =  $('#twitnews li').fir开发者_Python百科st();
    }
    else {
    var nextel = $(visel).next();
    }

HTML: (dynamically generated but here is the source from one page)

<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>


This works as is if you're using jquery 1.6:

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

if you're below 1.6 then you need to change this:

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

The ability to pass a jquery object to is was added in 1.6. You would need to do something like this:

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

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


You're missing the $

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

精彩评论

暂无评论...
验证码 换一张
取 消