简体   繁体   English

jQuery Mobile文档准备好替代

[英]JQuery Mobile document ready alternative

I spent hours trying to debug a jquery mobile page I had created and finally got it to work. 我花了几个小时尝试调试我创建的jquery移动页面,最后使其工作。 I had put the script it in the data-role="page" class="page-map4" div. 我已经将脚本放在data-role =“ page” class =“ page-map4” div中。 I was wondering why this was the case, I would rather this not be magic to me. 我想知道为什么会这样,我宁愿这对我来说不是魔术。 This didn't matter if I accessed the page directly, only if I had accessed it from another JQuery Mobile page. 如果直接访问该页面并不重要,仅当我从另一个JQuery Mobile页面访问它时才如此。

$('.page-map4').live("pageshow", function(){
        if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(success, error);
        } else {
              error('Geolocation not supported');
        }
});

Question: Why does the above script have to be in the page data-role div to work correctly when navigated from another JQuery Mobile page? 问题:当从另一个JQuery Mobile页面导航时,为什么上面的脚本必须在页面数据角色div中才能正常工作?

For page events I simply do this in my jQuery code. 对于页面事件,我只需在jQuery代码中执行此操作即可。 This goes in a <script> tag-set in the <head> element of a page, after any document binding to the $.mobile global: 在将任何文档绑定到$.mobile全局文件之后,这会在页面的<head>元素中的<script>标记集中进行:

$('#YOUR_PAGE_ID').live('pageshow',function(event, ui){
  yourGeoCode();
});

您应该使用pageinit事件,请参阅本文“ 使用jQuery Mobile –记住使用pageinit()而不是$(document).ready() ”以获取更多信息。

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

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