2016-02-12 33 views
2

我正在使用的網站是使用jQuery的路點版本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(); 

,但我不能因爲有其他wyapoints使用destroyAll我不想銷燬的頁面。理想的情況是我能做到在每個航點基礎上,像這樣:

foo.destroy(); 

或者至少是上下文的基礎上。這裏有什麼問題,我遵循文檔,但沒有得到預期的結果。也許是因爲我使用的是舊版本?

+0

你刪除'this.destroy()'從處理程序在調用'foo.destory()'之前,等等? –

+0

@RobM。在很多情況下,當我嘗試銷燬它們以避免處理程序代碼執行時,路標並未被解僱。儘管如此,我仍然試圖從處理程序中刪除'this.destroy()',同樣'foo.destroy不是函數錯誤。 – TK123

回答

1

是一個數組,所以你需要寫:

foo[0].destroy(); 

而且從imakewebthings.com/waypoints考慮這個問題:

There is one major difference between using the $.fn.waypoint method with versions 2.0 and 3.0. In 2.0, the same jQuery object was returned for chaining purposes, as is common among core jQuery methods. In 3.0, an array of Waypoint instances is returned.

+0

哈哈哦,夥計,謝謝。這件愚蠢的事讓我感到非常悲傷。 – TK123

+0

另一個小技巧:'var ctx = Waypoint.Context.findByElement($('#myElem'));'會返回undefined,因爲你傳遞了一個jQuery對象,但它期望一個常規的HTMLElement。 '$('#myElem')[0]'會給你正確的參數。 – imakewebthings

相關問題