关闭菜单和加载页面

时间:2013-06-19 14:57:37

标签: html5 jquery-mobile cordova

我正在使用带有Phonegap的jQuery mobile 我创建了一个加载到#content anther html文件的“母版页”index.html 我还有一个我想用于导航的滑动菜单。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Computer World</title>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css"/>
    <!--link rel="stylesheet" href="css/jquery.mobile-1.3.1.min.css" /-->
    <link rel="stylesheet" href="css/styles.css"/>
    <!--script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script-->
    <!--script type="text/javascript" src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script-->
    <script type="text/javascript" src="script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="script/jquery.mobile-1.3.1.min.js"></script>
    <script type="text/javascript">

    function loadPage(page){
        $('#content').load(page);
    }

    $(document).on('pageinit', function(){
        console.log("pageinit");
        loadPage('main.html');
    });

</script>

</head>
<body>
<div data-role="page" id="home" data-theme="b">

    <div id="header" data-role="header" data-theme="b">
        <a id="bars-button" data-icon="bars"  class="ui-btn-right" style="margin-top:10px;" href="#navpanel">Menu</a>
    </div>

    <div id="content" data-role="content">

    </div>

    <div data-role="panel" id="navpanel" data-theme="a" data-display="push" data-position="right">
        <div data-role="controlgroup" data-corners="false">
            <a href="Main.html" data-role="button">Main</a>
            <a href="#navpanel" onclick="loadPage('business.html')" rel="external">Business</a>
            <a href="numbers.html" data-role="button">Numbers</a>
            <a href="money.html" data-role="button">Money</a>
            <a href="people.html" data-role="button">People</a>
        </div>
    </div>
</div>
</body>
</html>

当我使用<a href="Main.html" data-role="button">Main</a>时,它会转到另一页 但是当我使用<a href="#navpanel" onclick="loadPage('business.html')" rel="external">Business</a>时,我得到了下一个例外:

Uncaught TypeError: Cannot read property 'options' of undefined
a
e.widget._create
(anonymous function)
e.Widget._createWidget
e.widget._createWidget
(anonymous function)
e.(anonymous function).(anonymous function)
(anonymous function)
b.extend.each
b.fn.b.each
e.fn.(anonymous function)
e.widget.enhance
(anonymous function)
e.widget.enhanceWithin
(anonymous function)
(anonymous function)
b.event.dispatch
v.handle
b.event.trigger
(anonymous function)
b.extend.each
b.fn.b.each
b.fn.extend.trigger
e.Widget._trigger
e.Widget._createWidget
e.widget._createWidget
(anonymous function)
e.(anonymous function).(anonymous function)
(anonymous function)
b.extend.each
b.fn.b.each
e.fn.(anonymous function)
r
e.mobile.changePage
e.mobile.gradeA.e.extend.initializePage
(anonymous function)
c
p.fireWith
b.extend.ready

我要做的是将外部文件内容加载到#content并关闭菜单。 我做错了什么?

1 个答案:

答案 0 :(得分:3)

这将永远不会起作用,因为两个动作都会互相残杀。在一方面,您正试图加载面板本身:

<a href="#navpanel"......

href 不能像这样指向自己,应该删除它。

第二件事 onClick 事件:

onclick="loadPage('business.html')"

根本不能那样使用。 loadPage 就像经典的jQuery函数加载一样是异步方法。在页面完全加载之前,基本上不会发生更改页面。这就是为什么需要在这里使用回调函数的原因。

更不用说使用2种不同的加载程序。有时,loadPage用于通过ajax将另一个页面加载到 DOM ,另一方面 rel="external" 告诉 {{ 1}} 刷新所有内容并打开另一页。

只需使用正常的页面加载方式:

jQuery Mobile

或者像这样:

<a href="business.html">Business</a>

或者还有另一种选择。可以预取另一个 <div data-role="panel" id="navpanel" data-theme="a" data-display="push" data-position="right"> <div data-role="controlgroup" data-corners="false"> <a href="Main.html" data-role="button">Main</a> <a href="business.html">Business</a> <a href="numbers.html" data-role="button">Numbers</a> <a href="money.html" data-role="button">Money</a> <a href="people.html" data-role="button">People</a> </div> </div> 页面。它可以在官方文档 HERE 中找到。

如果你这样做:

HTML

只要页面 <a href="business.html" data-prefetch>Business</a> 处于有效状态,就会确保已加载buiness.html。