Pageinit触发多次 [英] Pageinit fires multiple times

查看:183
本文介绍了Pageinit触发多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发jQuery Mobile和PhoneGap应用程序.我正在使用此代码:

I am developing a jQuery Mobile and PhoneGap app. I am using this code:

$('#contact').live('pageinit', function() {
    //$.mobile.loading('show');
    theme();
    getData('contact/list',contactList);
    //$.mobile.loading('hide');
});

首次访问页面时,效果很好.在第二次尝试中,事件触发了多次.我尝试使用bind,但是它不起作用.

When accessing page for the first time, it works good. In second attempt event is firing multiple times. I tried using bind but it doesn't work.

我认为它与live事件有关.每次我初始化页面时,它都会被绑定,这使页面变得多个.以这种方式链接时,问题已解决:window.location.href-它重新创建DOM.不幸的是我无法使用它.

I think it is connected with live event. It is binded each time I initialize the page, which makes it multiple. Problem is solved when linking that way: window.location.href-it recreates DOM. Unfortunately I can't use it.

是否有其他方法可以处理pageinit?

Is there any way to handle pageinit in another way?

我尝试过早找到它,但没有成功.还查看了: click()多次触发

I tried to find it earlier but with no success. Also looked at: click() firing multiple times

推荐答案

从理论上讲,可以由实时"绑定的任何事件都可以直接绑定.直接绑定的好处是它将(iirc)覆盖先前的绑定处理程序.这样,您只有一个处理程序,因此在后续加载时不会多次触发该程序.

In theory, any event that can be bound by 'live' can be bound directly. The benefit of binding directly is that it will (iirc) overwrite the previous bound handler. As such, you would only have one handler, so it wouldn't get triggered multiple times on subsequent loading.

尝试类似的东西:

$("#contact").pageInit(function() {
  theme();
  getData('contact/list', contactList);
});

这篇关于Pageinit触发多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆