2012-01-15 59 views
1

基於時間我一個一個地顯示div。<hr> divs

我這樣做與jQuery與

$("#section_"+index).show(); 

我的div已編號的,如: section_1,section_2,section_3等

我有VAR currentSection這是最後一個是的id所示。 現在我想要顯示的最後一個和前一個之間的hr(水平標尺)。 如果它顯示下一個舊的hr應該被刪除,並且應該顯示新的hr。

這可以用jQuery來完成嗎?

+0

「section_x」作爲ID是可怕的 – Raynos 2012-01-15 13:02:41

+0

這可以使用純CSS完成:'[id^=「section」]:last-of-type {border-top:2px solid#000;}'。 – 2012-01-15 13:06:17

+0

@RobW在IE上不支持。 (<= 8) – 2012-01-15 13:08:03

回答

2

在你的CSS,添加:

.latest { 
    border-top: 1px solid #888; 
} 

在你的JS,改變你展現聲明:

$('.latest').removeClass('latest'); 
$("#section_"+index).addClass('latest').show(); 
+0

我推薦'[id^=「section _」]:last'而不是'#section_'; – 2012-01-15 13:13:50

+0

是的,那也可以。即使索引在當時還不知道。 – techfoobar 2012-01-15 13:16:48

+0

感謝,清楚和容易。 – clankill3r 2012-01-16 19:19:05

0

除了techfoobar's answer,你可以做這樣的事情:

$.fn.showNext = function(selector, delay) { 
    var previous = this; 
    var next = this.next(); 

    if (next.length > 0) { 
     setTimeout(function() { 
      previous.removeClass('latest'); 
      next.addClass('latest').show().showNext(selector, delay); 
     }, delay); 
    } 
}; 

$('.section').first().show().showNext('.section', 2000); 

這樣,你不需要那些不切實際的section_n索引。演示here on JSFiddle

+0

謝謝,我需要section_n更多的東西,但你的帖子無論如何是有幫助的。 – clankill3r 2012-01-16 19:18:43