Jquery - $.mobile.changePage 不工作

Jquery - $.mobile.changePage is not working

我正在尝试更改项目中的页面。我有两个包含信息的 div,它们每个都有 "page" 的数据角色。此外,他们都有 id。在我的 js 文件中,我有一个简单的 onclick 事件,它调用一个函数,然后使用

          $.mobile.changePage("#2");

从第一个 div 到第二个 div。唯一的问题是,每当我尝试更改页面时,我都会收到错误消息:

          Uncaught TypeError: Cannot read property 'changePage' of undefined

我已经搜索过其他人关于 stack overflow 的帖子,但没有找到任何可以帮助我的东西。这是我的一些代码:

global.html

    <html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, 
    maximum-scale=1.0, user-scalable=no">

    <script src="js/lib/jquery-2.1.4.js"></script>
    <link rel="stylesheet" type="text/css" href="css/lib/jquery.mobile- 
    1.4.5.css">


    <script src="js/global.js"></script>

    <script src="js/lib/jquery.mobile-1.4.5.js"></script>
    <script src="js/p5.min.js"></script>
      <script src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.8.21/jquery-ui.min.js"> 
    </script>
</head>

<body>
   <div data-role="page" id="1" style="background-color: #56f0b1;">
         <button id ="startGame" type="button">Start Game</button>
   </div>

   <div data-role="page" id="2" style="background-color: #56f0b1;">
         <h2>Page 2 </h2>
   </div>

</body>
</html>

global.js

 $(document).ready(function () {
     $("#startGame").on("click",getToNextPage);
 });

 function getToNextPage(){
     $.mobile.changePage("#2");
 }

如评论中所述,核心 jQuery 库必须在 jQuery Mobile 之前加载。
在您的代码中,您似乎正在加载两个 jQuery 版本,其中一个是在 jQuery Mobile.

之后加载的

此外,根据 this question 中的评论,页面 ID 不应仅为数字。

这是一个例子:

$(document).ready(function() {
  $("#startGame").on("click", getToNextPage);
});

function getToNextPage() {
  $.mobile.changePage("#page2");
}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>


<div data-role="page" id="page1" style="background-color: #56f0b1;">
  <button id="startGame" type="button">Start Game</button>
</div>

<div data-role="page" id="page2" style="background-color: #56f0b1;">
  <h2>Page 2 </h2>
</div>