我有一组31个div,每个人都有一个数字类。我正在尝试完成它,以便在单击“提交”按钮时使用一类今天的日期或更早的动画。

<div class="image 1">SAMPLE TEXT</div>
<div class="image 2">SAMPLE TEXT</div>
<div class="image 3">SAMPLE TEXT</div>
<div class="image 4">SAMPLE TEXT</div>
<form>
   <input class="previous" type="submit" value="View Previous" />
</form>

而且这是一点点,但这远非工作,我一生都无法弄清楚原因。

$("form").submit(function() 
    {
    var number = $('div.image').attr("class").match(/\d+/),
        d = new Date(),
        day = d.getDate();

    if (number <= day){
        $('div.image').animate({"height":"0px", "width":"150px", "bottom":"75px"}, 500);
    }
    else {
    }
});

我目前拥有它,因此当您悬停它们时,每个单独的div都会动画,但是我希望在单击“提交”按钮时能够为其中的一组动画。谢谢!

有帮助吗?

解决方案

第一个:您必须从函数表格提交false(不要重新加载页面)返回false。然后

d = new Date(),
day = d.getDate();

$(".image").each(function(i,e){
    var number = $(e).attr("class").match(/\d+/);
    if (number <= day)
        $(e).addClass('active'); 
});

$('div.active').animate({"height":"0px", "width":"150px", "bottom":"75px"}, 500);

其他提示

因为表格在动画开始运行之前已经提交了,所以您应该添加 return false 动画之后

像那样

if (number <= day){

    $('div.image').animate({"height":"0px", "width":"150px", "bottom":"75px"}, 500);

    return false;
}

else {

}

为什么不在DIV上使用自定义属性?尝试这样的事情。

html:

<div class="image" number="1">SAMPLE TEXT</div>
<div class="image" number="2">SAMPLE TEXT</div>
<div class="image" number="3">SAMPLE TEXT</div>
<div class="image" number="4">SAMPLE TEXT</div>
<form>
   <input class="previous" type="submit" value="View Previous" />
</form>

JavaScript:

$('div.image').each(function()
{
    var number = $(this).attr('number'),
        d = new Date(),
        day = d.getDate();

    if (number <= day){
        $(this).animate({"height":"0px", "width":"150px", "bottom":"75px"}, 500);
    }
});

要测试上述内容,请将其添加到您的“ OnSubmit”或“ Onhover”事件处理程序中...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top