检测jquery mobile上的changepage

时间:2014-05-02 13:16:50

标签: javascript jquery jquery-mobile

当我使用.changePage()功能更改页面时,如何执行代码?

例如我使用此代码:

$.mobile.changePage( "index.html#pageTwo" );

当加载pageTwo时,我想这样做:

alert("Test");

1 个答案:

答案 0 :(得分:1)

在你的< head>中..< / head>把这个:


// Jquery loaded here

<script>
    $(document).on("pageshow","#pageTwo", function() {
        alert("Test");
    }
</script>

// jquery mobile loaded here

注意:

  1. 上面的代码必须在加载Jquery之后放置,并且在加载jquery mobile之前。

  2. 上面的代码示例假设您的index.html页面包含至少具有data-role =&#34; page&#34;属性的div。和id =&#34; pageTwo&#34;:

    &lt; div data-role =&#34; page&#34; ID =&#34; pageTwo&#34;&GT;这是第二页内容&lt; / div&gt;

  3. Jquery mobile仅解析/使用&lt; head&gt; ..&lt; / head&gt;中的任何内容第一页加载的部分!为了确保加载移动网站中所有页面所需的所有代码,无论用户首先登陆哪个页面,您都应该构建所有页面,如下所示:

  4.     <html>
        <head>
           <script src="path/to/jquery.js"> // load jquery
           <script src="path/to/common.js"> // all jquery mobile page event bindings are placed in here
           <script src="path/to/jquery_mobile.js"> // load jquery mobile
        </head>
        <body>
    
        <div data-role="page" id="pageOne"> This is page one content </div>
    
        <div data-role="page" id="pageTwo"> This is page two content </div>
    
        </body>
        </html>