gpt4 book ai didi

jQuery Mobile 在 PageCreate 上更改页面

转载 作者:行者123 更新时间:2023-12-01 04:15:40 26 4
gpt4 key购买 nike

大家好。

我想更多地了解 jQuery mobile,所以我昨天就开始了,但我已经陷入了困境。

我想要实现的是:

  • 加载index.html
  • index.html 重定向到pages/home.html

这样,pages/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>

最佳答案

有可能,由于将事件绑定(bind)到文档而发生奇怪的行为,每次加载页面时都会触发 pagecreate 事件。 (第一次是从index到pages/home.html,第二次是从pages/home.html到pages/home.html)为了避免这个问题,请在索引页中设置一个 ID,如下所示,并将事件绑定(bind)到 #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

希望这可以帮助你。

关于jQuery Mobile 在 PageCreate 上更改页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15040853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com