2016-09-13 25 views
0

我有一個帶有滑塊功能的網站,其中有一個下一個按鈕。當沒有其他幻燈片時,我想要解除先前的點擊功能並使用單擊按鈕執行其他操作。首先從插件中取消綁定舊點擊功能,然後綁定新的功能

這裏是JS

jQuery(document).bind('DOMSubtreeModified', function() { 
    var button = jQuery("._disabled").length; 
    if (button) { 
    jQuery("._next").off("click"); 
    jQuery("._disabled").on("click", handler); 
    } 
    var handler = function (e) { 
    e.preventDefault(); 
    jQuery('.suggested').show(); 
    jQuery('.theiaPostSlider_slides').html(''); 
    jQuery('.theiaPostSlider_slides').append(jQuery('.suggested')); 
    jQuery('._buttons').hide();  
    } 
}); 

按鈕的HTML是在這裏:

<a rel="next" href="#" class="_button _next _disabled"> 
    <span class="_1">Next</span> 
    <span class="_2"><span aria-hidden="true" class="tps-icon-chevron-circle-right"></span></span> 
    <span class="_3"></span> 
</a> 

這是網站:link 當您單擊一次下一步按鈕,它工作正常,然後當它是在最後一張幻燈片上,它應該解除綁定然後綁定新的功能,但它只能解除綁定。

我在這裏做錯了什麼?

+1

爲什麼你有一些凱特·​​阿普頓的網站爲你鏈接?你錯誤地粘貼了嗎?你可以看看並做適當的編輯。 – ShellZero

+0

請創建一個[mcve]並將其添加到您的問題 – ochi

+0

@ShellZero我很確定這是他的網站只是scrolldwon,並擊中綠色的下一個按鈕,你會看到他在說什麼 – finalfreq

回答

0

您使用.on.off的方式,你可以使用.bind.unbind

如果您的jQuery版本小於3的方式,你可以把它改成這樣:

jQuery("._next").unbind("click"); 
    jQuery("._disabled").bind("click", handler); 

否則,您可以使用正確的格式保留.on.off。事情是這樣的:

jQuery('body').off("click", "._next"); 
    jQuery('body').on("click", "._disabled", handler); 

更多信息:

http://api.jquery.com/unbind/

http://api.jquery.com/off/

+0

是的,我忘了切換回去,但它不工作 – OndrejK