jQuery Mobile经典... 登陆
jQuery Mobile经典教程
作者:php中文网  更新时间:2022-04-11 13:58:34

jQuery Mobile 页面事件


jQuery Mobile 页面事件

在 jQuery Mobile 中与页面打交道的事件被分为四类:

  • Page Initialization - 在页面创建前,当页面创建时,以及在页面初始化之后

  • Page Load/Unload - 当外部页面加载时、卸载时或遭遇失败时

  • Page Transition - 在页面过渡之前和之后

  • Page Change - 当页面被更改,或遭遇失败时

如需关于所有 jQuery Mobile 事件的完整信息,请访问我们的 jQuery Mobile 事件参考手册。


jQuery Mobile Initialization 事件

当 jQuery Mobile 中的一张典型页面进行初始化时,它会经历三个阶段:

  • 在页面创建前

  • 页面创建

  • 页面初始化

每个阶段触发的事件都可用于插入或操作代码。

事件描述
pagebeforecreate当页面即将初始化,并且在 jQuery Mobile 已开始增强页面之前,触发该事件。
pagecreate当页面已创建,但增强完成之前,触发该事件。
pageinit当页面已初始化,并且在 jQuery Mobile 已完成页面增强之后,触发该事件。

下面的例子演示在 jQuery Mobile 中创建页面时,何时触发每种事件:

实例

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).on("pagebeforecreate",function(){
  alert("pagebeforecreate 事件触发 - 页面即将初始化。jQuery Mobile 还未增强页面");
});                     
$(document).on("pagecreate",function(){
  alert("pagecreate 事件触发 - 页面已经创建,但还未增强完成");
});
</script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
    <h1>头部文本</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>页面已创建,并增强完成。</p>
  </div>

  <div data-role="footer">
    <h1>底部文本</h1>
  </div>
</div> 

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


jQuery Mobile Load 事件

页面加载事件属于外部页面。

无论外部页面何时载入 DOM,将触发两个事件。第一个是 pagebeforeload,第二个是 pageload (成功)或 pageloadfailed(失败)。

下表中解释了这些事件:

事件描述
pagebeforeload在任何页面加载请求作出之前触发。
pageload在页面已成功加载并插入 DOM 后触发。
pageloadfailed如果页面加载请求失败,则触发该事件。默认地,将显示 "Error Loading Page" 消息。

下列演示 pageload 和 pagloadfailed 事件的工作原理:

实例

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).on("pagecontainerload",function(event,data){
  alert("pagecontainerload 事件触发!\nURL: " + data.url);
});
$(document).on("pagecontainerloadfailed",function(event,data){
  alert("抱歉,被请求页面不存在。");
});
</script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
    <h1>页眉文本</h1>
  </div>

  <div data-role="main" class="ui-content">
    <a href="externalpage.html">外部页面</a>
    <br><br>
    <a href="externalnotexist.html">外部页面不存在</a>
  </div>

  <div data-role="footer">
    <h1>页脚文本</h1>
  </div>
</div> 

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


jQuery Mobile 过渡事件

我们还可以在从一页过渡到下一页时使用事件。

页面过渡涉及两个页面:一张"来"的页面和一张"去"的页面 - 这些过渡使当前活动页面("来的"页面)到新页面("去的"页面的改变过程变得更加动感。

事件描述
pagebeforeshow在"去的"页面触发,在过渡动画开始前。
pageshow在"去的"页面触发,在过渡动画完成后。
pagebeforehide在"来的"页面触发,在过渡动画开始前。
pagehide在"来的"页面触发,在过渡动画完成后。

下列演示了过渡时间的工作原理:

实例

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).on("pagebeforeshow","#pagetwo",function(){
  alert("触发 pagebeforeshow 事件 - 页面二即将显示");
});
$(document).on("pageshow","#pagetwo",function(){
  alert("触发 pageshow 事件 - 现在显示页面二");
});
$(document).on("pagebeforehide","#pagetwo",function(){
  alert("触发 pagebeforehide 事件 - 页面二即将隐藏");
});
$(document).on("pagehide","#pagetwo",function(){
  alert("触发 pagehide 事件 - 现在隐藏页面二");
});
</script>
</head>
<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>页眉文本</h1>

  </div>

  <div data-role="main" class="ui-content"> 
    <p>页面一</p>
    <a href="#pagetwo">转到页面二</a>
  </div>

  <div data-role="footer">
    <h1>页脚文本</h1>
  </div>
</div> 

<div data-role="page" id="pagetwo">
  <div data-role="header">
    <h1>页眉文本</h1>

  </div>

  <div data-role="main" class="ui-content">
    <p>页面二</p>
    <a href="#pageone">转到页面一</a>
  </div>

  <div data-role="footer">
    <h1>页脚文本</h1>
  </div>
</div> 

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


PHP中文网