【发布时间】:2014-10-07 17:24:35
【问题描述】:
在 jQuery mobile 中,我想从外部文件加载页面容器。我可以将标记添加到我的 DOM,但之后我面临的问题是,一旦我导航到 #page2,整个 #page1-div 就会从 DOM 中消失,因此我无法返回(请参阅下面的屏幕截图)。
点击“转到第 2 页”之前的 DOM
点击“转到第 2 页”后的 DOM
如您所见,整个#page1-div 已经一去不复返了。这是为什么?有什么想法吗?请在下面查看我的标记和代码:
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.css" />
<title>Hello jqm</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script>
<script>
$(document).ready(function(){
$(document).on( "pagecontainerload", function( event, ui ) {
console.log('navigating to page1...');
$.mobile.pageContainer.pagecontainer("change", "#page1");
console.log('navigating done!');
} );
console.log('loading pagecontainers...');
$.mobile.pageContainer.pagecontainer("load", "page1.html");
$.mobile.pageContainer.pagecontainer("load", "page2.html");
console.log('pagecontainer-load done!');
});
</script>
</body>
</html>
page1.html
<div data-role="page" id="page1">
<div data-role="header">
<h1>Page 1</h1>
</div>
<div role="main" class="ui-content">
<a href="#page2" data-transition="slide" class="ui-btn ui-corner-all ui-btn-inline">Go To Page 2</a>
</div>
</div>
page2.html
<div data-role="page" id="page2" class="page2">
<div data-role="header">
<h1>Page 2</h1>
</div>
<div role="main" class="ui-content">
<a href="#page1" data-rel="back" data-transition="slide" class="ui-btn ui-corner-all ui-btn-inline">Go Back To Page 1</a>
</div>
</div>
备注:这是对该问题的后续问题:jQuery mobile - Splitting up pages to different files
【问题讨论】:
-
您介意创建一个小提琴以便更好地理解吗?
-
我很乐意,但 jsfiddle 似乎不适用于这些导入(至少在我的保管箱中公开托管 page1.html 和 page2.html 时我无法让它工作)。但是,我将上面标记中的所有外部链接都替换为链接公共 cdns,因此您只需将代码复制并粘贴到硬盘上的三个本地文件中,然后打开 test.html 即可在浏览器中查看结果。
标签: javascript jquery html jquery-mobile dom