Jquery 完成onclick show div时隐藏按钮

Jquery 完成onclick show div时隐藏按钮,jquery,onclick,hide,Jquery,Onclick,Hide,一、 我是jquery的绝对支持者。我用谷歌学习,但我不知道如何解决这个简单的问题 我用一个按钮连续显示div,但是当没有更多div要显示时,我想隐藏show按钮 代码如下: $('#show').on('click', function() { if (index + 1 > max) { // Here, I suppose, I need to hide the button } else { if (index < max -

一、 我是jquery的绝对支持者。我用谷歌学习,但我不知道如何解决这个简单的问题

我用一个按钮连续显示div,但是当没有更多div要显示时,我想隐藏show按钮

代码如下:

$('#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();
        }
    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()   ;
    }
});
$('#show')。在('click',function()上{
如果(索引+1>最大值){
//在这里,我想,我需要隐藏按钮
}否则{
如果(索引<最大值-1){
索引++;
$('.container>div:eq('+index+')).show();
}
在这里你可以看到这个例子


提前感谢并对我的英语感到抱歉;)

您可以通过检查每次单击按钮时是否显示最后一个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();
        }
    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()   ;
    }
});
var指数=-1;
var max=$('.container>div').length;
$('#show')。在('单击',函数()上){
如果(索引+1>最大值){
//无所事事
}否则{
如果(索引<最大值-1){
索引++;
$('.container>div:eq('+index+')).show();
}
}
if($('div.hidden:last').css('display')!='none'){
$('#show').hide();
}
});

基本上,您需要在完成以下功能后执行此操作

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();
});
var指数=-1;
var max=$('.container>div').length;
$('#show')。在('单击',函数()上){
如果(索引<最大值-1){
索引++;
$('.container>div:eq('+index+')).show();
}
},函数(){
$(this.hide();
});

这应该适用于您

迭代索引后,检查回调函数中
.show()
的值减去
2
。这将导致预期的行为。之所以是
-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();
            }
        });
    }
});
var索引=-1
var max=$('.container>div').length;
$('#show')。在('click',函数(){
var$this=$(this);
如果(指数<最大值){
索引++;
$('.container>div:eq('+index+'))。显示(0,函数(){
如果(索引>最大值-2){
$this.hide();
}
});
}
});

如果您的情况(单击次数最多),只需调用.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();
});
var指数=-1;
var max=$('.container>div').length;
$('#show')。在('单击',函数()上){

如果(--max如果您是jquery的新手,我非常感谢您的工作,非常好,您的代码几乎接近您想要的:

你需要这个代码:

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();
        }   
});
var指数=0;
var max=$('.container>div').length;
$('#show')。在('单击',函数()上){
如果(指数<最大值){
$('.container>div:eq('+index+')).show();
索引++;
如果(索引>=最大值)
$(this.hide();
}   
});

awamome!!很好地解决了这个问题,我理解。非常感谢,如果我能帮助你,我会继续学习接受我的答案。