Suppose I want to run some code once jQuery Mobile has finished rendering the UI. The mobileinit event doesn't work since it's raised before this happens. A quick Google search seems to indicate that simply using $(document).ready won't work with JQM; but I just tried it (called after mobileinit) and it worked for me: my code ran and dynamically updated elements, etc. just fine. So I'm wondering, is there some reason I shouldn't be using it (it's unreliable or messes up JQM), or is the information out there about it not working simply inaccurate? What am I missing?

假设我想在jQuery Mobile完成渲染UI后运行一些代码。 mobileinit事件不起作用,因为它在此之前被提升。快速谷歌搜索似乎表明仅使用$(document).ready将无法与JQM一起使用;但我只是尝试了它(在mobileinit之后调用)它对我有用:我的代码运行并动态更新元素等等就好了。所以我想知道,是否有一些原因我不应该使用它(它不可靠或混淆JQM),或者那里有关于它的信息根本不准确?我错过了什么?

Update: See here for a demonstration.

更新:请参阅此处进行演示。

1 个解决方案

#1


12

Most likely the reason you read that $(document).ready won't work with jQuery Mobile is that it does not fire each time you view a pseudo-page. That said, it still triggers as it should when the html document is loaded.

很可能是你读到$(document).ready无法与jQuery Mobile一起工作的原因是每次查看伪页面时它都不会触发。也就是说,它仍然会在加载html文档时触发。

If you want to run code that triggers each time you view a pseudo-page you can use this code:

如果要运行每次查看伪页面时触发的代码,可以使用以下代码:

$('[data-role="page"]').live('pageshow', function () {
    //run your code here
});

NOTE: there are other hooks that you can bind to as well (pageshow, pagehide, pagebefoershow, pagebeforehide), documentation can be found here: http://jquerymobile.com/demos/1.0b1/docs/api/events.html

注意:还可以绑定其他钩子(pageshow,pagehide,pagebefoershow,pagebeforehide),文档可以在这里找到:http://jquerymobile.com/demos/1.0b1/docs/api/events.html

---------- EDIT ----------

----------编辑----------

I was thinking about this and the best analog to $(document).ready() is not binding to the "pageshow" event, it would be binding to the "pagecreate" event. $(document).ready() fires once per page load, and "pagecreate" does the same for pseudo-pages whereas "pageshow" fires each time a page is displayed.

我正在考虑这个和$(document).ready()的最佳模拟不绑定到“pageshow”事件,它将绑定到“pagecreate”事件。 $(document).ready()每次加载时触发一次,“pagecreate”对伪页面执行相同操作,而每次页面显示时都会触发“pageshow”。

So if a user clicked away from the home-screen and then clicked a back button to return to the home-screen, "pageshow" would fire on this second (and subsequent) "showing" of the home-screen.

因此,如果用户点击远离主屏幕,然后点击后退按钮返回主屏幕,则“页面显示”将在第二(以及随后)“显示”主屏幕时触发。

Also, "pageshow" requires the user to navigate to the page to which it is bound.

此外,“pageshow”要求用户导航到其绑定的页面。

更多相关文章

  1. 基于jQuery实现页面搜索功能----项目实战
  2. jquery 触发a标签点击事件,弹出页面
  3. 如何在jQuery Datatables分页中返回特定页面?
  4. jQuery File Upload 单页面多实例的实现
  5. 如何使用CSS将表放在页面的中心?
  6. 如何在HTML中为移动页面创建图像下载链接?
  7. html 页面内锚点定位及跳转方法总结
  8. xml格式原样输出到html或是jsp页面
  9. HTML5-Service Worker实现离线页面访问

随机推荐

  1. 红茶一杯话Binder(传输机制篇_下)
  2. Linux(Android):如何禁用Intel DPST(显示节电
  3. 【原创】安卓程序员的大革命,Cocovr框架库
  4. Android开发中MinSDK与TargetSDK不在同一
  5. android项目 之 记事本(14) ----- 手势缩放
  6. 使用RecyclerView的AppBarLayout可以在不
  7. 更新后-崩溃com.google.android.gms:play
  8. Android 进阶:Fragment 源码深入理解
  9. 显示操作栏和向上导航 - Android
  10. Android View事件传播机制