处理 JQuery :last 选择器
我试图检测这是否是列表的最后一个元素。这段代码不起作用。什么会?
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>
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').first();
}
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 jquery 1.6,则效果如下:
http://jsfiddle.net/Da8ja/2/
如果您低于 1.6,那么您需要更改此设置:
将 jquery 对象传递给
is
的功能是在 1.6 中添加的。您需要执行以下操作:http://jsfiddle.net/Da8ja/3/
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:
The ability to pass a jquery object to
is
was added in 1.6. You would need to do something like this:http://jsfiddle.net/Da8ja/3/
您缺少
$
You're missing the
$