【问题标题】:jquery function run once inside eventjquery函数在事件内运行一次
【发布时间】:2015-04-04 05:38:54
【问题描述】:

我正在使用 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');
          }
    });

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

【问题讨论】:

  • 你在哪里定义了onLeave。你能展示一下整个功能吗?
  • 对不起,我不知道如何显示:(,因为 onLeave 事件带有 fullpage.js 插件。

标签: jquery events fullpage.js


【解决方案1】:

最简单的方法可能是只选择那些同时具有ab 类的元素。

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

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

没关系。在正常大小的 DOM 中,当 b 已经被删除时,尝试删除它几乎没有任何惩罚。如果 DOM 很大,那么,是的,想办法分离回调。

请注意,无论你最终做什么,最好在if(){...} 子句中使用 jQuery 编写函数:

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

【讨论】:

  • 非常感谢,它的工作,两种方法也工作,非常好:) 我现在在 if(){} 里面写 jquery,谢谢你的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多