2011-05-14 86 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