为什么我不能摧毁()这些航点

Why can't I destroy() these waypoints?

本文关键字:不能 摧毁 为什么      更新时间:2023-09-26

我正在研究的网站使用的是jquery waypoints版本3.1.1。我可以像这样从它的处理程序中销毁一个航点:

var foo = $('#myElem').waypoint({
    handler: function() {
        // do something
        this.destroy();
    },
    offset: 'bottom-in-view'
});

但不是这样的:

foo.destroy();

我收到错误:

foo.destroy is not a function

我也不能从上下文基础销毁它:

var ctx = Waypoint.Context.findByElement($('#myElem'));
ctx.destroy();

我收到错误:

Cannot read property 'destroy' of undefined

我可以销毁航点的唯一方法是从处理程序内部或使用 destroyAll:

Waypoint.destroyAll();

但我不能使用 destroyAll,因为页面上还有其他我不想销毁的 wyapoints。理想情况下,我可以在每个航点的基础上做到这一点,如下所示:

foo.destroy();

或者至少在上下文的基础上。这里有什么问题,我正在关注文档但没有得到预期的结果。也许是因为我使用的是旧版本?

foo是一个数组,所以你需要写:

foo[0].destroy();

还要从 imakewebthings.com/waypoints 考虑这一点:

版本 2.0 和 3.0 中使用 $.fn.waypoint 方法之间存在一个主要区别。在 2.0 中,出于链接目的返回了相同的 jQuery 对象,这在核心 jQuery 方法中很常见。在 3.0 中,返回一个 Waypoint 实例的数组。