javascript - 如何在 JQuery Mobile 多页面模板结构中,将第二页显示为默认页?

  显示原文与译文双语对照的内容
93 3

如何将第二页显示为 JQuery Mobile 多页模板结构中的默认页?

<body> 
<div data-role="page" id="foo">
 <div data-role="header">
 <h1>Foo</h1>
 </div>
 <div data-role="content"> 
 <p>I'm first in the source order so I'm shown as the page.</p> 
 </div>
</div>
<div data-role="page" id="home">
 <div data-role="header">
 <h1>Home</h1>
 </div>
 <div data-role="content"> 
 <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p> 
 </div>
</div>
<div data-role="page" id="bar">
 <div data-role="header">
 <h1>Bar</h1>
 </div>
 <div data-role="content"> 
 <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p> 
 </div>
</div>
</body>
时间:原作者:0个回答

124 1

例子

工作示例:http://jsfiddle.net/Gajotres/UfDaf/

使用的代码

这里部分将阻止正常页面加载。不要忘记,比如在我的示例 mobileinit 必须在 jQuery Mobile 之前初始化。

$(document).on("mobileinit",function() {
 $.mobile.autoInitializePage = false;
}); 

这里部分将启动手动页面初始化:

$(document).ready(function() { 
 window.location.hash = 'home';
 $.mobile.initializePage();
});

我通常给建议在这里使用 document ready usage我们需要它来启动手动变更。

完整代码示例

如果 jsFiddle down

HTML:

<!DOCTYPE html>
<html>
 <head>
 <title>jQM Complex Demo</title>
 <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
 <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
 <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css"/>
 <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
 <script>
 $(document).on("mobileinit",function() {
 $.mobile.autoInitializePage = false;
 }); 
 </script>
 <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> 
 </head>
 <body>
 <div data-role="page" id="foo">
 <div data-role="header">
 <h1>Foo</h1>
 </div>
 <div data-role="content"> 
 <p>I'm first in the source order so I'm shown as the page.</p> 
 </div>
 </div>
 <div data-role="page" id="home">
 <div data-role="header">
 <h1>Home</h1>
 </div>
 <div data-role="content"> 
 <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p> 
 </div>
 </div>
 <div data-role="page" id="bar">
 <div data-role="header">
 <h1>Bar</h1>
 </div>
 <div data-role="content"> 
 <p>I'm the second in the source order so I'm hidden when the page loads. I'm just shown if a link that references my id is beeing clicked.</p> 
 </div>
 </div> 
 </body>
</html> 

JavaScript:

$(document).ready(function() { 
 window.location.hash = 'home';
 $.mobile.initializePage();
});
原作者:
116 4

我知道这个问题有点旧,但我偶然发现了一个简单的解决方案:

window.location.href ="_dash.html#page-two";

或者者,如果你从独立的页面导航,你可以以制作一个链接,在第二页中删除u:

<a href="_dash.html#page-two">link to second page</a>

只需在url的末尾添加第二页的页面id即可

原作者:
...