点击jQuery没有使用相同的类进行划分

时间:2014-10-09 12:43:05

标签: jquery jquery-animate this

修改

我已经让我们说了50个分区,所有这些都是班级.tablestyle
我想看看我是否点击一个特定的部门,但仍然使用.tablestyle执行我的动画。

如果我只想在第一个分区执行动画,那很容易:

  $(document).ready(function() {
    $(".tablestyle").first().on("click", function() {
        $("#advertentieorchidee").animate({left: 0}, 600);
    });
});

但我也喜欢在2到5分区执行另一个动画。

$(document).ready(function() {
        $(".tablestyle").secondtillfive???().on("click", function() {
            $("#advertentieorchidee").animate({left: 0}, 600);
        });
    });

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

如果我理解你需要。

$(document).ready(function() {
    $(".tablestyle:lt(5)").on("click.animate", function() { //Bind event for less than 5
        $("#advertentieorchidee").animate({left: 0}, 600);
        $(this).off("click.animate"); //Remove click handler, thus used namespace event handler
    });
});

更新了代码

$(document).ready(function() {
    function eventHandler() { 
        //Animate
        $("#advertentieorchidee").animate({left: 0}, 600);

        //Remove click handler, thus used namespace event handler
        $(".tablestyle").off("click.animate"); 

        //Get Index
        var index = $(".tablestyle").index( this );

        //Bind event for interval Index less than 5
        $(".tablestyle:gt(" + index +"):lt(5)").on("click.animate", eventHandler);            
    }

    //Bind event for greater than 1 less than 5
    $(".tablestyle:gt(1):lt(5)").on("click.animate", eventHandler);
});

答案 1 :(得分:0)

closest找到了先例。 document是根元素。所以,它不可能是任何先例。所以将代码更改为

$(document).ready(function() {
    $(".tablestyle").on("click", function() {
        $("#advertentieorchidee").animate({left: 0}, 600);
    });
});

答案 2 :(得分:0)

$(document).ready(function() {
    $(".tablestyle").first().on("click", function() {
        $("#advertentieorchidee").animate({left: 0}, 600);
    });
});

注意:如果要将click事件绑定到整个.first()集合,请删除.tablestyle