2011-05-14 93 views
1

我試圖檢測這是否是列表的最後一個元素。此代碼無效。什麼會?處理JQuery:最後一個選擇器

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

可以粘貼在這裏你的HTML? – 2011-05-14 15:11:47

+0

現在完成問題 – 2011-05-14 15:40:17

+0

檢查'$('#twitnews li')'在firebug中的值。看看它有多少元素返回 – 2011-05-14 15:41:46

回答

2

這工作如如果你使用jquery 1.6:

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

如果你是低於1.6,那麼您需要更改此:

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

到jQuery對象傳遞給is在1.6加入的能力。你需要做這樣的事情:

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

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

-1

你缺少$

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

是的,我看到了,但仍然無法正常工作 – 2011-05-14 15:22:59

+0

粘貼html .. – 2011-05-14 15:24:35

+0

完成,現在有問題 – 2011-05-14 15:40:11