【问题标题】:Link fails to work unless refreshing除非刷新,否则链接无法工作
【发布时间】:2013-07-01 11:50:06
【问题描述】:

我目前正在使用 JQuery mobile 开发一个移动网站。我在foobar.html 中使用多个页面进行如下导航:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="style/jquery.mobile-1.3.1.css" />
    <script src="jquery-js/jquery-1.10.1.js"></script>
    <script src="jquery-js/jquery.mobile-1.3.1.js"></script>
    <title>Foobar</title>
</head>

<body>
    <div data-role="page" id="foo">
        <div data-role="content">
            <a href="#bar" data-role="button">Go to Bar</a>
        </div>
    </div>

    <div data-role="page" id="bar">
        <div data-role="content">
            <p>Bar</p>
        </div>
    </div>
</body>

我加载了foobar.html 文件,点击转到栏,它工作正常;但是,当我从 index.hmtl 导航到 foobar.html 并再次测试时,链接无法正常工作。刷新页面即可解决问题。

什么可以解释这种行为以及如何解决它?

【问题讨论】:

    标签: html jquery-mobile


    【解决方案1】:

    说明

    使用 jQuery Mobile 和多个 HTML 文件时,重要的是要了解当您从 index.html 转到 foobar.html ** 将加载第一页。基本上 jQuery Mobile 会删除页面的其余部分,包括 HEAD 和其余的 BODY 内容。

    因此,当处理多个 HTML 页面时,只有首页可以有多个内页,所有其他 HTML 页面可以有ONLY强> 1页。在您的情况下,只有页面 #foo 被加载到 DOM 中,页面 #bar 被丢弃。

    另外,我还有另一个 ARTICLE,其中描述了 jQuery Mobile 如何处理多个 HTML 页面加载。

    概念证明

    index.html

    <!DOCTYPE html>
        <html lang="en">
            <head>
                <!-- META TAGS Declaration -->
                <meta charset="UTF-8">
                <title>TEst</title>
                <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0;" />
                <meta name="apple-mobile-web-app-capable" content="yes" />
                <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 src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>  
                <script>
                $(document).on('pagebeforeshow', '#foo', function(){ 
                    alert($('#body-test').html());
                });
                </script>           
            </head>
    
            <body id="body-test">
                <div data-role="page" id="portfolio"  data-add-back-btn="true">             
                    <div data-role="content" data-theme='c' >
                        <a href="test.html" data-role="button">Go to Bar</a>
                    </div>
                </div><!-- Page Project #1 -->
            </body>     
    </html>
    

    test.html

    <html>
        <head>
            <meta charset="utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1"> 
            <link rel="stylesheet" href="style/jquery.mobile-1.3.1.css" />
            <script src="jquery-js/jquery-1.10.1.js"></script>
            <script src="jquery-js/jquery.mobile-1.3.1.js"></script>
            <title>Foobar</title>
        </head>
    
        <body>
            <div data-role="page" id="foo">
                <div data-role="content">
                    <a href="#bar" data-role="button">Go to Bar</a>
                </div>
            </div>
    
            <div data-role="page" id="bar">
                <div data-role="content">
                    <p>Bar</p>
                </div>
            </div>
        </body>
    </html>
    

    如果你运行这个例子,它会告诉你(提醒你)只有页面 #foo 被加载了。

    【讨论】:

    • Thaks,为了简洁的解释。所以,我想解决这个问题的最简洁的方法是为每个 page 创建一个 html 文件?
    猜你喜欢
    • 2015-06-08
    • 2015-02-15
    • 1970-01-01
    • 2013-07-17
    • 2021-05-14
    • 2019-11-09
    • 1970-01-01
    • 2011-04-09
    • 2021-09-13
    相关资源
    最近更新 更多