Jquery mobile 将$.mobile.changePage放置在正确的位置

Jquery mobile 将$.mobile.changePage放置在正确的位置,jquery-mobile,Jquery Mobile,我试图了解$.mobile.changePage是如何工作的。我将方法$.mobile.changePage放在DOM中最后一个元素之后的匿名函数中,但它不起作用,但如果我将它放在document.ready中,它就可以正常工作。怎么会?非常感谢您的建议 <!DOCTYPE html> <html> <head> <title>My Page</title> <meta name="viewport" co

我试图了解$.mobile.changePage是如何工作的。我将方法$.mobile.changePage放在DOM中最后一个元素之后的匿名函数中,但它不起作用,但如果我将它放在document.ready中,它就可以正常工作。怎么会?非常感谢您的建议

<!DOCTYPE html> 
<html> 
<head> 
    <title>My Page</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head> 
<body> 

    <!-- Start of first page -->
    <div data-role="page" id="foo">

        <div data-role="header">
            <h1>Foo</h1>
        </div><!-- /header -->

        <div data-role="content">   
            <p>I'm first in the source order so I'm shown as the page.</p>      
            <p>View internal page called <a href="#bar">bar</a></p> 
        </div><!-- /content -->

        <div data-role="footer">
            <h4>Page Footer</h4>
        </div><!-- /footer -->

    </div><!-- /page -->

    <!-- Start of second page -->
    <div data-role="page" id="bar">

        <div data-role="header">
            <h1>Bar</h1>
        </div><!-- /header -->

        <div data-role="content">   
            <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p>      
            <p><a href="#foo">Back to foo</a></p>   
        </div><!-- /content -->

        <div data-role="footer">
            <h4>Page Footer</h4>
        </div><!-- /footer -->

    </div><!-- /page -->

    <script>
        (function(){ 

                $.mobile.changePage($("#bar"), { transition: "slideup"} );          

        })();// this doesn't work


        $(document).ready(function(){

                $.mobile.changePage($("#bar"), { transition: "slideup"} );

            })//this works

    </script>

我的页面
福
我是源代码顺序中的第一个,因此显示为页面。

查看名为

页脚 酒吧 我是源代码顺序中的第二个,所以当页面加载时我是隐藏的。如果点击了引用我id的链接,我就会显示出来。

页脚 (函数(){ $.mobile.changePage($(“#bar”),{transition:“slideup”}); })();// 这不管用 $(文档).ready(函数(){ $.mobile.changePage($(“#bar”),{transition:“slideup”}); })//这很有效
文档准备就绪
无法正确使用jQuery Mobile。通常,它会在页面加载到
DOM
之前触发

如果你想找到更多关于这方面的信息,请看一下这个,这是我的个人博客。或者找到它

要使其正常工作,您需要使用正确的页面事件,如下所示:

$(document).on('pagebeforeshow', '#foo', function(){       
    $.mobile.changePage($("#bar"), { transition: "slideup"} );
});

同时,这不是一个好的解决方案。您不应该在加载第一个页面时更改页面,这主要是因为这会导致jQuery Mobile行为不端。请在成功加载第一页后执行此操作(page
\foo
),或更改页面顺序,并让page
\bar作为第一页。

文档就绪
无法正确使用
jQuery Mobile
。通常,它会在页面加载到
DOM
之前触发

如果你想找到更多关于这方面的信息,请看一下这个,这是我的个人博客。或者找到它

要使其正常工作,您需要使用正确的页面事件,如下所示:

$(document).on('pagebeforeshow', '#foo', function(){       
    $.mobile.changePage($("#bar"), { transition: "slideup"} );
});
同时,这不是一个好的解决方案。您不应该在加载第一个页面时更改页面,这主要是因为这会导致jQuery Mobile行为不端。请在成功加载第一页(页面
#foo
)后执行此操作,或者更改页面顺序,并将页面
#bar
作为第一页