jquery mobile的两种页面跳转方式

目前在做使用jquerymobile + cordova来创建hybrid mobile app。

总结一下jquerymobile中的页面切换方式。

方式1:所有html都放在一个index.html中

 
  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <title>jQuery Mobile 多容器页面结构</title>

  5. <meta name="viewport" content="width=device-width,

  6. initial-scale=1"/>

  7. <link href="Css/jquery.mobile-1.0.1.min.css"

  8. rel="Stylesheet" type="text/css"/>

  9. <script src="Js/jquery-1.6.4.js"

  10. type="text/javascript"></script>

  11. <script src="Js/jquery.mobile-1.0.1.js"

  12. type="text/javascript"></script>

  13. </head>

  14. <body>

  15. <div data-role="page">

  16.  
  17. <div data-role="header"><h1>天气预报</h1></div>

  18.  
  19. <div data-role="content">

  20. <p><a href="#w1">今天</a> | <a href="#">明天</a></p>

  21.  
  22. </div>

  23.  
  24. <div data-role="footer"><h4>©2012 rttop.cn studio</h4></div>

  25.  
  26. </div>

  27.  
  28.  
  29. <div data-role="page" id="w1" data-add-back-btn="true">

  30.  
  31. <div data-role="header"><h1>今天天气</h1></div>

  32.  
  33. <div data-role="content">

  34. <p>4~-7℃<br/>晴转多云<br/>微风</p>

  35.  
  36. </div>

  37.  
  38. <div data-role="footer"><h4>©2012 rttop.cn studio</h4></div>

  39.  
  40. </div>

  41. </body>

  42. </html>

方式2:多个html页面相互切换

 
  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <title>jQuery Mobile 外部页面链接</title>

  5. <meta name="viewport" content="width=device-width,

  6. initial-scale=1"/>

  7. <link href="Css/jquery.mobile-1.0.1.min.css"

  8. rel="Stylesheet" type="text/css"/>

  9. <script src="Js/jquery-1.6.4.js"

  10. type="text/javascript"></script>

  11. <script src="Js/jquery.mobile-1.0.1.js"

  12. type="text/javascript"></script>

  13. </head>

  14. <body>

  15. <div data-role="page">

  16. <div data-role="header"><h1>天气预报</h1></div>

  17. <div data-role="content">

  18. <p><a href="#w1">今天</a> | <a href="#">明天</a> | <a href="#">后天</a></p>

  19. </div>

  20. <div data-role="footer"><h4>©2012 rttop.cn studio</h4></div>

  21. </div>

  22. <div data-role="page" id="w1" data-add-back-btn="true">

  23. <div data-role="header"><h1>今天天气</h1></div>

  24. <div data-role="content">

  25. <p>4~-7℃<br/>晴转多云<br/>微风</p>

  26. <em style="float:right;padding-right:5px">

  27. <a href="about.htm" rel="C">rttop.cn</a>提供

  28. </em>

  29. </div>

  30. <div data-role="footer"><h4>©2012 rttop.cn studio</h4></div>

  31. </div>

  32. </body>

  33. </html>

about.htm内容

 
  1. <div data-role="page" data-add-back-btn="true">

  2. <div data-role="header"><h1>关于rttop</h1></div>

  3. <div data-role="content">

  4. <p>   

  5. rttop.cn是一家新型高科技企业,正在努力实现飞翔的梦想。

  6. </p>

  7. </div>

  8. <div data-role="footer"><h4>©2012 rttop.cn studio</h4></div>

  9. </div>

jqm页面切换机制mechanism

对于方式1,,在一个页面中,不论相同框架的“page”容器有多少,只要对应的Id 号唯一,就可以通过内部链接的方式进行容器间的切换。在切换时, jQuery Mobile 会在文档中寻找对应“Id”的容器, 然后通过动画的效果切换到该页面中。

对于方式2,单击一个指向外部页面的超级链接(如about.htm) , jQuery Mobile 将自动分析该URL 地址,自动产生一个AJAX 请求。在请求过程中,会弹出一个显示进度的提示框。如果请求成功, jQueryMobile 将自动构建页面结构,注入主页面的内容,同时,初始化全部的jQuery Mobile 组件,将新添加的页面内容显示在浏览器中,如果请求失败,jQuery Mobile 将弹出一个错误信息提示框,数秒后该提示框自动消失,页面也不会刷新。

如果不想采用AJAX 请求的方式打开一个外部页面,只需要在链接元素中将“rel”属性设置为“external”,或data-ajax=false,该页面将脱离整个jQueryMobile 的主页面环境.以独自打开的页面效果在浏览器中显示。

如果采用AJAX 请求的方式打开一个外部页面,注入主页画的内容也是以“page ”为目标,“page”以外的内容将不会被注入主页函中:另外,必须确保外部加载页面URL 地址的唯一性。这也就是说明,其他页面只要写div data-role=page就可以,相同的head不需要重写写,即css及js文件只需要在index.html中引用一次就可以。

不过要注意的是,如果一旦没有按照jquery mobile默认的ajax方式进行切换,那么页面就无法加载head内容了,比如在js文件中使用window.location.href= “about.htm”,就只加载了about.htm中div的内容。解决方案就是在js文件中仍要使用jqm的页面切换方式

 
  1. //window.location.reload();

  2. $.mobile.changePage(about.htm, {

  3. allowSamePageTransition: true,

  4. transition: 'none',

  5. reloadPage: true,

  6. });

原文链接:https://blog.csdn.net/u013905744/article/details/77050830/

猜你喜欢

转载自blog.csdn.net/jarniyy/article/details/81183353