繁体   English   中英

使用 jquery 移动设备首次加载页面后事件不起作用

[英]Events not working after the page loads for the first time using jquery mobile

我正在使用 JQM 1.1.0 和 Cordova 1.5.0。

我有这样的代码

<!DOCTYPE HTML>
<html>
<head>
<title>Index Page</title>

<!-- Adding viewport -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Adding Phonegap scripts -->
<script type="text/javascript" charset="utf-8"
src="cordova/cordova-1.5.0.js"></script>

<!-- Adding jQuery mobile and jQuery scripts & CSS -->
<script type="text/javascript" src="jquery/jquery-1.7.1.min.js"></script>
<link rel="stylesheet"
href="jquerymobile/jquery.mobile-1.1.0-rc.1.min.css" />
<script type="text/javascript" src="jquery/jquery.validate.min.js"></script>
<script type="text/javascript"
src="jquerymobile/jquery.mobile-1.1.0-rc.1.min.js"></script>
<link rel="stylesheet" href="css/colors.css">

<script type="text/javascript">

$("#page1").live("pageinit", function(e) {
console.log("pageinit1");
    }

$("#page2").live("pageinit", function(e) {
console.log("pageinit2");
    }
</script>
<body>
<div data-role="page" id="page1">
  <div data-role="header">Page 1</div>
  <div data-role="content"></div>
</div>

<div data-role="page" id="page2">
  <div data-role="header">Page 2</div>
  <div data-role="content"></div>
</div>
</body>

此代码适用于 Android。但在 iOS 中,一旦页面加载,page1 的 pageinit 事件就不会被触发,稍后如果我将页面更改为 page2,那么一切都很好。为什么会这样?

对此我不太确定,但据我所知,live function 在 JQ1.7.x 中发生了变化,现在您将不得不使用 on() function,它对您编写代码的方式进行了微小的更改。

我只需要在 data-role="page" 之后编写我的 javaScript 函数就可以让它工作......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM