【发布时间】:2019-03-29 09:43:46
【问题描述】:
我正在尝试更改项目中的页面。我有两个包含信息的 div,它们每个都有一个“页面”的数据角色。此外,它们都有 id。在我的 js 文件中,我有一个简单的 onclick 事件,它调用一个函数,然后使用
$.mobile.changePage("#2");
从我的第一个 div 转到我的第二个 div。唯一的问题是每当我尝试更改页面时都会出现错误:
Uncaught TypeError: Cannot read property 'changePage' of undefined
我已经搜索过其他人关于堆栈溢出的帖子,但没有找到任何可以帮助我的东西。这是我的一些代码:
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 Mobile 之前包含 jQuery 核心库。见Mobile Page Structure。
-
你的jquery库需要先于移动库
-
我相信它已经是了。我在 之前有 >
-
我的意思是,在您提供的代码中,
jquery.mobile-1.4.5.js包含在jquery.min.js之前。这两个需要以相反的顺序加载。 -
请注意,混合使用 jQuery-UI 和 jQueryMobile 是一个高级主题,两个库都有自己的小部件,在您以正确的方式操作之前,您会遇到一些意想不到的行为。
标签: javascript jquery html jquery-mobile