2013-04-15 42 views
0

我在初始化JQuery Mobile站点上的事件时遇到了一些问题,我只想看看我是否正确地做了这件事。这里是我的代码有点样子:JQuery Mobile:页面初始化事件时遇到问题

<!DOCTYPE html> 
<html> 
    <head> 
     <title>My page</title> 

     <link rel="stylesheet" href="jquery.mobile-1.3.0-beta.1.css" /> 
     <script src="jquery-1.9.0.js"></script> 
     <script src="jquery.mobile-1.3.0-beta.1.js"></script> 
     <script text/javascript> 
      function myFunction() 
      { 
       //do some code 
      } 
     </script> 
     </head> 
     <body> 
      <div data-role="page" id="home"> 
       <div data-role="header"> 
        <h1>Home</h1> 
       </div> 

       <div data-role="content" data-theme="f"> 
        <p><a href="#next-page" data-role="button" data-transition="fade">2nd page</a></p>   
       </div> 
      </div> 
      <div data-role="page" id="next-page"> 
       <div data-role="header"> 
        <h1>2nd page</h1> 
       </div> 
       <div data-role="content" data-theme="f"> 
        <script text/javascript> 
         $("#next-page").on("pageinit", function() { 
          myFunction(); 
         }); 
        </script> 
       </div> 
      </div> 
     </body> 
</html> 

出于某种原因,当我加载了第二页,我只是得到了AJAX加载图标,它不会停止加载。

另一件事是,我不想myFunction()被调用,直到第二页被加载,这就是为什么我有第二页内的脚本。

任何帮助是非常赞赏:)

回答

1

JavaScript是关键敏感,如果你调用函数myfuncion()然后同样的功能必须存在。

在你的情况你的功能有一个大的F在其名称:)myFunction的(,因为调用的函数myfuncion()不存在浏览器将报告错误和jQuery Mobile将停止执行。

在另一方面,这种代码:如果你想在第二页初始化过程中执行此功能

$("#next-page").on("pageinit", function() { 

}); 

是确定的。 Jzst有个小小的建议,改成这样:

$(document).on("pageinit", "#next-page",function() { 

}); 

编辑:

解决您的问题pageshow事件取代你pageinit事件。 jQuery Mobile在处理其他“图形”框架(地图,轮播) 时遇到问题,它们通常只能在pageshow事件中使用。

$(document).on("pageshow", "#next-page",function() { 

}); 
+0

我的错,我现在纠正了这个问题。这不是我的实际代码,所以这不是问题。 – mrpopo

+0

忽略拼写错误,这不是我的页面的问题,因为这只是我输入的代码来显示我的问题的示例 – mrpopo

+0

然后,您的代码没有任何问题:http://jsfiddle.net/Gajotres/V4NyB/你可以看到它工作得很好。 – Gajotres