jQuery mobile popup on pageinit

99封情书 提交于 2019-12-31 00:49:09

问题


I want a popup to open as soon as the page loads but seem to be getting stuck with the spinning wheel.

Here is a fiddler to demonstrate the problem any help would be appreciated.

http://jsfiddle.net/Ohpyx/UGfXG/

The code I'm using is:

$(document).live('pageinit',function(event){
    $('#popupBasic').popup('open');
})​

回答1:


This worked for me:

$(document).on('pageinit', '.ui-page',function(event){
    setTimeout(function () {
        $('#popupBasic').popup('open');
    }, 0);//Note the comment below from @Taifun.
})​

You had a race condition and this places the popup code at the end of the queue.

Here is a demo: http://jsfiddle.net/UGfXG/6/

Note: I replaced .live() with .on() (the delegated flavor) as the former has been depreciated as of jQuery 1.7.




回答2:


The .popup('open') needs the $.mobile.activePage, which is set after the pageinit event. The pagechange event seems to be better for popups.

This worked for me :

$(document).on('pagechange',function(event){
    $('#popupBasic').popup('open');
})​

If you want it just at the first load, use .one :

$(document).one('pagechange',function(event){
    $('#popupBasic').popup('open');
})​

See https://github.com/jquery/jquery-mobile/issues/3384



来源:https://stackoverflow.com/questions/13772868/jquery-mobile-popup-on-pageinit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!