简体   繁体   English

取消绑定展开JQuery Mobile折叠

[英]Unbind expand JQuery Mobile Collapse

I am using a JQuery Mobile Accordion list 我正在使用JQuery Mobile Accordion列表

I'm listening for an expansion of one of the items using (in my pageshow listener) 我正在使用(在我的pageshow侦听器中)监听其中一项的扩展

$("#myListSection").bind("expand",function(){})

In my app, pages are switched back and forth. 在我的应用中,页面来回切换。 I had a problem where multiple "expand" subscriptions would be created so when I would switch to the list again I'd have multiple functions being called when I expanded a section. 我有一个问题,其中会创建多个“扩展”订阅,因此当我再次切换到列表时,扩展一个部分时会调用多个函数。 Therefore, I want to use the unbind method when leaving the page to destroy the listener. 因此,我想在离开页面时使用unbind方法破坏监听器。

The problem I'm having is when I unbind the event and then try and rebind. 我遇到的问题是当我取消绑定该事件然后尝试重新绑定时。

$("#myListSection").unbind("expand")
$("#myListSection").bind("expand")

Basically there is now only one expand event being fired (which is good), but the list section doesn't expand... I'm thinking the unbind destroyed more than just my listener. 基本上,现在只触发了一个expand事件(这很好),但是list部分并没有扩展...我认为取消绑定的破坏不仅仅是我的听众。

JS Fiddle example: http://jsfiddle.net/6txWy/1658/ JS Fiddle示例: http : //jsfiddle.net/6txWy/1658/

Simple. 简单。 Set the functions you would like to execute as separate functions like this : 将要执行的功能设置为单独的功能,如下所示:

function expand() {..}
function collapse() {..}

Then your event handlers must be like this : 然后,您的事件处理程序必须是这样的:

$('#my-collaspible').bind({
     'expand': expand,
     'collapse': collapse
});

I prefer passing functions like this because it makes my code clean and understandable. 我更喜欢传递这样的函数,因为它使我的代码清晰易懂。 THEN , let's come to the main part. 然后 ,进入主要部分。 Instead of just using unbind and removing all the functionality associated with the collapsible, use unbind with the function you previously bound it to like this : 而不是仅仅使用unbind ,并删除与可收缩相关联的所有功能,使用unbind与你以前绑定它喜欢这个功能:

$(this).unbind('expand', expand);

where the second expand is the anon function you passed. 第二个expand是您传递的anon函数。 Later, when needed, the collapsible could be bound back to expand() like this. 稍后,在需要时, collapsible可以这样绑定回expand() (again) (再次)

$(this).bind('expand', expand);

(Assuming this is the collapsible here) (假设this是可折叠的)

Demo : http://jsfiddle.net/hungerpain/6txWy/1661/ 演示: http : //jsfiddle.net/hungerpain/6txWy/1661/

Extras - why jQuery deletes all event data associated with collapsible when unbind is used : Collapsibles aren't native are they? 附加功能-为什么在使用unbind时jQuery删除与可折叠相关的所有事件数据 :可折叠不是本地的吗? They're fabricated by jQM by using a toggleClass, which, is in a function in itself and attached as a click event handler. 它们是由jQM通过使用toggleClass来构造的,它本身是一个函数,并附加为click事件处理程序。 (Not completely how it works but you get the gist) So by just calling unbind you're destroying everything . (它的工作方式并不完全,但是您可以理解要点)因此,仅通过调用unbind破坏一切

Hope this helps :) 希望这可以帮助 :)

PS Please consider using the latest version of jQuery & jQuery mobile. PS请考虑使用最新版本的jQuery&jQuery mobile。 There's a new kid in the block named on which is far more better than bind in a lot of ways. 在很多方面,有一个新的孩子在这个名字onbind要好得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM