js中的JQUERY移动加载页面

时间:2013-06-04 14:08:03

标签: jquery-mobile

我有一个简单的index.html,有两页......

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css">
  <script src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
  <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
</head>
<body>

<div data-role="page" id="page1">
  <div></div>
</div>

<div data-role="page" id="page2">
  <div></div>
</div>
</body>
</html>

Page1有一个按钮..当用户点击它时,我调用js函数。

function openPage2() {
}

此函数应加载page2。怎么做?

我试过

$.mobile.changePage("page2.html");

但它不起作用......我做错了什么?

1 个答案:

答案 0 :(得分:2)

工作示例:http://jsfiddle.net/Gajotres/gQbLw/

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>    
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
    <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/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
</head>
<body>
    <div data-role="page" id="page1">
        <div data-theme="a" data-role="header">
            <h3>
                First Page
            </h3>
        </div>

        <div data-role="content">
            <a href="" data-role="button" id="next-page">Next</a>
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div> 
    <div data-role="page" id="page2">
        <div data-theme="a" data-role="header">
            <h3>
                Second Page
            </h3>
            <a href="#index" class="ui-btn-left">Back</a>
        </div>

        <div data-role="content">

        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">

        </div>
    </div>    
</body>
</html>   

Javascript:

$(document).on('pagebeforeshow', '#page1', function(){       
    $(document).on('click', '#next-page', function(){     
        $.mobile.changePage("#page2");
    });        
});