jquery函数在事件内部运行一次

时间:2015-04-04 05:38:55

标签: jquery events fullpage.js

我正在使用fullpage.js插件,并且在事件onLeave中有一个函数,这里是jquery代码

  onLeave: function(index, nextIndex, direction) {
        $('#section-first').find('.a').each(function() {  // i want run once then off / remove this function
          if (index >= 2 && direction == 'up') {
            $(this).removeClass('b');
          }
    });

它不是点击功能,我不知道如何在运行一次后关闭/删除此功能,仍然使用开/关?但我把它放在哪里?非常感谢:)。

1 个答案:

答案 0 :(得分:2)

最简单的方法可能只选择同时包含a类和b类的元素。

onLeave: function(index, nextIndex, direction) {
    $('#section-first .a.b').each(function() {
        if (index >= 2 && direction == 'up') {
            $(this).removeClass('b');
        }
    });
}

这样,除非将class="a b"添加到#section-first的新元素添加到b,否则该函数仍会运行但不会执行任何操作。

并不重要。在正常大小的DOM中,当已经删除类if(){...}时,尝试删除类onLeave: function(index, nextIndex, direction) { if (index >= 2 && direction == 'up') { $('#section-first .a.b').removeClass('b'); } } 几乎没有任何惩罚。如果DOM非常大,那么,是的,找到一些方法来分离回调。

请注意,无论你最终做什么,都应该在{{1}}子句中使用jQuery更好地编写函数:

{{1}}