jQuery Mobile - Do something on page load

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

I want to do something every time a page loads. It's something that fixes the way the mobile site looks on different devices so it needs to happen on AJAX loads too.

At the moment, I've tried the traditional $(function(){ /*...*/ }); approach but that only works on the first load, and not subsequent AJAX loads.

I've been looking for the right event to bind to but I'm failing pretty hard.

回答1:

You can use JQuery to bind to each "page" (div's set with the data-role=page attribute) and use the pageshow event (there are some others as well: pagebeforeshow, pagehide, pagebeforehide).

$(document).delegate('.ui-page', 'pageshow', function () {     //Your code for each page load here }); 

http://api.jquerymobile.com/pageshow/

Note: this answer was written for jQuery Mobile 1.0.1, see the documentation link above for more information.



回答2:

You can listen to the pageshow or pagecreate event and do your work there.

http://jquerymobile.com/test/docs/api/events.html



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!