Javascript 如何在jQuery mobile中启用onload(从链接和加载打开页面)?

Javascript 如何在jQuery mobile中启用onload(从链接和加载打开页面)?,javascript,jquery,ajax,jquery-mobile,Javascript,Jquery,Ajax,Jquery Mobile,我想有链接,打开其他网页,并有onload功能。我知道在jQueryMoblie中,它使用Ajax导航,只在第一次加载DOM,而忽略其他页面标题 第1页(链接到第2页)->第2页 我研究发现我需要使用: $(document).on("pageinit",function(){ alert("pageinit event fired - the page has been initialized, the DOM has been loaded and jQuery Mobile has f

我想有链接,打开其他网页,并有onload功能。我知道在jQueryMoblie中,它使用Ajax导航,只在第一次加载DOM,而忽略其他页面标题

第1页(链接到第2页)->第2页

我研究发现我需要使用:

$(document).on("pageinit",function(){
  alert("pageinit event fired - the page has been initialized, the DOM has been loaded and jQuery Mobile has finished enhancing the page")
}); 

$(document).on("pagebeforecreate",function(){
  alert("pagebeforecreate event fired - the page is about to be initialized. jQuery Mobile has not begun enhancing the page");
});                     

$(document).on("pagecreate",function(){
 alert("pagecreate event fired - the page has been created, but enhancement is not complete");
});
我在第二页的页眉上使用它(也放在
之间)。当我加载页面时,它不起作用。只有在我刷新页面时才起作用。 我通过使用外部导航解决了这个问题,但是我破坏了ajax导航

我是否需要使用
$.mobile.changePage()
$.mobile.load()
,以及在第1页上的确切位置?我找到了很多信息,但没有具体的解释和示例说明如何在第1页和第2页上放置事件,以便在单击链接时启用onload功能,而不刷新

谢谢

更新:

我试过这个方法,它对我很有效。我想分享一下其他人是否也有同样的问题

 $(document).on("pageinit","#page1",function(){
        alert("onloadtesting page 1-pageinit event fired - the page has been initialized, the DOM has been loaded and jQuery Mobile has finished enhancing the page")
        }); 

$(document).on("pageinit","#page2",function(){
        alert("onloadtesting page 2-pageinit event fired - the page has been initialized, the DOM has been loaded and jQuery Mobile has finished enhancing the page")
        }); 
在第1页的开头,也添加到第1页

在第2页:

如果要添加更多页面事件,只需在页面中添加id,并在AJAX导航启动时在第一页中添加事件。只有第一页的页眉会被称为。由于AJAX导航,所有其他页面的页眉都将被忽略。(除非您使用的是a href rel=external)

(注意:页面刷新不起作用,因为它破坏了导航模型,对于页面刷新,页面刷新时将触发页面本身的页面初始化)


另外,解释jQuery移动事件的伟大博客:

将库放在第一页的
标题中。其他页面的任何额外js,都放在page div中。感谢Omar。您的回答引导我找到了这一点,它可以工作:第1页标题-'$(文档)。on(“pageinit”,“#page2”,function(){alert(“pageinit事件触发”))“;”->当我单击第二页“$(文档)开头的link.on(“pageinit”,function(){alert(“pagecreate onloadtesting3事件激发”);}时,就会发生这种情况'->当页面自行加载时会发生这种情况。但是有更好的方法吗?如果我有很多页面,可能会让人困惑。jQM设计为使用单个页面模板,也就是说,将所有页面放在一个文件中。如果要使用多页面,则在head中加载核心JS库和其他库(针对特定页面)更安全在这些页面中。谢谢!你应该用一个例子来回答。我想同时使用多页和单页。这对于多页来说是有意义的,但对于很多单页来说似乎很沉重。因为我需要将它们的页面初始化放在第一个初始页面上(当我按下链接而不刷新页面时,pageinit可以工作).如果我有5页或更多的内部链接,可能会令人困惑。