jQuery检查最后一个子句是否正确语法

时间:2017-09-26 07:38:11

标签: jquery

我写了一些jQuery,它使用两个if语句,在尝试执行更多代码之前检查当前元素是最后一个子节点还是第一个子节点。我使用的当前语法是:

<script>
    $('#right').on('mousewheel', function(e) {

        if (e.originalEvent.wheelDelta >= 0) {

            if (current.is(':first-child') == false) {
                $('.scroll-item.active').removeClass('active').prev('.scroll-item').addClass('active');
            }

        } else {

            if (current.is(':last-child') == false) {
                $('.scroll-item.active').removeClass('active').next('.scroll-item').addClass('active');
            }

        }

        current = $('.scroll-item.active');
        next = current.next('.scroll-item');

    });
</script>

我很好奇是否有办法使用.not()代替is.()检查该元素是不是:last-child还是:first-child?我目前的方式似乎有点矫枉过正。

1 个答案:

答案 0 :(得分:1)

你也可以这样做:

if (!current.is(':first-child')) { ... }

&安培;

if (!current.is(':last-child')) { ... }