2013-02-23 96 views
2

Good'ay to all you。jQuery Mobile在PageCreate上更改页面

我以为我想了解更多关于jQuery手机的知识,所以我昨天就开始了,但是我一开始就陷入困境。

我想实现的是:

  • index.html的负载
  • 的index.html重定向到页/ home.html的

这样一来,网页/ home.html的会成为默认页面。这是否有可能? 目前,我有这样的:

<body> 
    <script> 
    $(document).bind('pagecreate', function() 
    { 
     $.mobile.changePage("pages/home.html"); 
    }); 
    </script> 
</body> 
</html> 

它显示了一种奇怪的行为,滑动两次,并显示错误消息(说页面无法加载)。

我希望我的所有页面都在页面子目录中。这是可能的还是我不可能再次?

谢谢。

编辑

其他页面在正文中包含以下内容;

<div data-role="page"> 

    <div data-role="header"> 
     <h1>Page Title</h1> 
    </div> 

    <div data-role="content"> 
     <p>Page content goes here.</p> 
    </div> 

</div> 
+0

我可以有这两个页面的来源,我只是想运行它在小提琴 – Ranjith 2013-02-23 13:17:51

+0

添加到编辑。 – Roel 2013-02-23 13:21:38

回答

2

有可能,奇怪的行为发生是因为绑定事件到文档,pagecreate事件将在每次页面加载时触发。 (第一次是从索引到pages/home.html,第二次是从pages/home.html到pages/home.html) 要避免此问题,请在您的索引页中设置一个id,并将事件绑定到#indexPage而不是文档。

<div data-role="page" id="indexPage"> 

<script type="text/javascript"> 
    $("#indexPage").bind('pagecreate', function() 
    { 
    $.mobile.changePage("pages/home.html"); 
    }); 
</script> 

请注意$ .mobile.changePage()只有在您的html被放置到主机/本地服务器(localhost)时才有效。如果你不想把文件放到服务器上。有一种替代方法,使用window.location而不是$ .mobile.changePage()。因为$ .mobile.changePage()将通过使用ajax加载html而不是在浏览器中刷新整个页面,以编程方式从一个页面更改为另一个页面。

<script type="text/javascript"> 
$("#indexPage").bind('pagecreate', function() 
{ 
    window.location = "pages/home.html; 
}); 
</script> 

如需详细步骤,请指http://demanstudio.blogspot.com/2013/02/change-pages-in-jquery-mobile.html

希望这可以帮助你。

+0

非常感谢。考虑它,这是有道理的。 – Roel 2013-02-23 15:09:26

0

这里的index.html

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="EUC-KR"> 
<title>Insert title here</title> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" /> 
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script> 
</head> 
<body> 
<script> 
    $(document).bind('pagecreate', function() 
    { 
     $.mobile.changePage("home.html"); 
    }); 
    </script> 

</body> 
</html> 

这里是home.html的本地服务器

<div data-role="page"> 

    <div data-role="header"> 
     <h1>Page Title</h1> 
    </div> 

    <div data-role="content"> 
     <p>Page content goes here.</p> 
    </div> 

</div> 

运行它,不要直接在浏览器中打开。它的工作正常

+0

如果它的工作请勾选答案,我想为我的配置文件提高堆栈流量 – Ranjith 2013-02-23 13:37:21

+0

它力求解决目的 – Ranjith 2013-02-23 13:46:22

+0

发生了什么没有响应 – Ranjith 2013-02-23 14:28:28

相关问题