2013-01-20 69 views
2

我,與jquery絕對begginner。我使用谷歌學習,但我不知道如何解決這個肯定容易的問題。隱藏按鈕,當我完成我的onclick顯示div

我是連續顯示div的按鈕,但是我想在沒有更多div顯示時隱藏顯示按鈕。

這是代碼:

$('#show').on('click', function() { 
    if (index + 1 > max) { 
     // Here, I suppose, I need to hide the button 
    } else { 
     if (index < max - 1) { 
      index++; 
      $('.container > div:eq(' + index + ')').show(); 
     } 

在這裏,你可以看到的例子提前

http://jsfiddle.net/vcARq/

感謝和我的英語很抱歉;)

回答

0

你可以這樣做通過檢查每次單擊按鈕時是否顯示最後的div。所以,如果顯示最後一個div,它將隱藏按鈕。下面的代碼:

var index = -1; 
var max = $('.container > div').length; 
$('#show').on('click', function() { 
    if (index + 1 > max) { 
     // Do Nothing 
    } else { 
     if (index < max - 1) { 
      index++; 
      $('.container > div:eq(' + index + ')').show(); 
     } 


    } 
    if ($('div.hidden:last').css('display') != 'none'){ 
    $('#show').hide() ; 
    } 
}); 
+0

經過精心解決,我明白了。非常感謝。 – Cosimo

+0

如果我幫你,請接受我的回答。 –

1

基本上你需要做這個完整的運行這樣

var index = -1; 
var max = $('.container > div').length; 
$('#show').on('click', function() { 
     if (index < max - 1) { 
      index++; 
      $('.container > div:eq(' + index + ')').show(); 
     } 
}, function() { 
$(this).hide(); 
}); 

的功能應該爲你工作

+0

精彩解決,我明白了。非常感謝。 – Cosimo

0

後您遍歷索引,檢查值減2裏面的回調函數爲.show()。這將導致預期的行爲。原因是-2是因爲你開始在-1而不是0的抵消。

var index = -1 
var max = $('.container > div').length; 
$('#show').on('click', function() { 
    var $this = $(this); 
    if (index < max) { 
     index++; 
     $('.container > div:eq(' + index + ')').show(0, function() { 
      if (index > max - 2) { 
       $this.hide(); 
      } 
     }); 
    } 
}); 
你的病情

Fiddle.

+0

出色地解決了,我明白了。非常感謝。 – Cosimo

0

我的情況下(點擊最多次數)只需調用.hide()。 這裏我簡單的建議:

var index = -1; 
var max = $('.container > div').length; 
$('#show').on('click', function() { 
    if (--max<=0) {   //condition 
     $('#show').hide(); //hide button 
    } 
    index++; 
    $('.container > div:eq(' + index + ')').show(); 
}); 

http://jsfiddle.net/vcARq/8/

+0

出色地解決了,我明白了。非常感謝。 – Cosimo

0

如果你是新來的jQuery我很欣賞你的工作,布拉沃和好的嘗試你的代碼幾乎接近什麼ü想:

Here is JSFiddle

你需要下面的代碼:

var index = 0; 
var max = $('.container > div').length; 
$('#show').on('click', function() { 
     if (index < max) { 

     $('.container > div:eq(' + index+')').show(); 
       index++; 
      if (index >= max) 
       $(this).hide(); 
     } 
}); 
+0

Awasome!精彩地解決了,我明白了。非常感謝,我會繼續學習 – Cosimo