想要在显示最后一个div时隐藏下一个按钮

时间:2014-01-14 09:01:20

标签: javascript html button hide

你能帮助我吗?我想在显示最后一个div时隐藏按钮? 当显示下一个div时,按钮隐藏并不再显示2个div。

$(document).ready(function()
{

    $("#next").click(function()
    {
        if ($(".divs div:visible").next().length != 0)
        {
            $(".divs div:visible").next().show().prev().hide();

            if($('.divs').find('div').last())
            {
                 $("#next").hide();
             }
        }

        return false;
    });
}

html:


<div class="divs">
    <div class="cls1">1</div>
    <div class="cls2">2</div>
    <div class="cls3">3</div>
    <div class="cls4">4</div>
    <div class="cls5">5</div>
    <div class="cls6">6</div>
    <div class="cls7">7</div>
</div>
<a id="next">next</a>

2 个答案:

答案 0 :(得分:1)

问题在于您的代码:

if($('.divs').find('div').last())
        {
             $("#next").hide();
         }

首次点击时会返回true,导致第一次点击按钮时隐藏按钮。你需要做的是检查可见div之后是否没有元素,当你隐藏它时。这应该可以解决问题:

$("#next").click(function()
{
    if ($(".divs div:visible").next().length != 0)
    {
        $(".divs div:visible").next().show().prev().hide();
        if ($(".divs div:visible").next().length == 0)
        {
            $("#next").hide();
        }
    }

    return false;
});

答案 1 :(得分:0)

$("#next").click(function()
{
    if ($(".divs div:visible").next().length != 0)
    {
        $(".divs div:visible").next().show().prev().hide();
    }
    else {
       $('#next').hide();
       return false;
    }
});
}
相关问题