【问题标题】:Programmatic (as opposed to link-based) navigation in jQueryMobilejQueryMobile 中的程序化(相对于基于链接的)导航
【发布时间】:2012-04-04 16:23:48
【问题描述】:

这个问题已经讨论过好几次了,但我还没有看到一个具体的答案。

类似/相同的问题:SO question

假设我有index.html:

    <div id="index1" data-role="page">
            <div data-role="header"><h1>Index 1</h1></div>
            <div data-role="content"></div>
            <div data-role="footer">
                <a data-role="button" id="toshop2">
                    To Shop 2
                </a>
            </div>
        </div>

shop.html:

<div id="shop1" data-role="page">
        <div data-role="header"><h1>Shop 1</h1></div>
        <div data-role="content"></div>
        <div data-role="footer">
            <a data-role="button" href="#shop2">To Shop 2</a>
            <a data-role="button" href="index.html" rel="external">
                To Index
            </a>
        </div>
    </div>
    <div id="shop2" data-role="page">
        <div data-role="header"><h1>Shop 2</h1></div>
        <div data-role="content"></div>
        <div data-role="footer">
            <a data-role="button" href="#shop1">To Shop 1</a>
            <a data-role="button" href="index.html" rel="external">
                To Index
            </a>
        </div>
    </div>

我想做类似的事情:

$('#toshop2').on('click', function() {
    $.mobile.changePage("shop.html#shop2");
});

我们现在都知道,这是行不通的。它会抓取第一个 DOM 页面(shop.html 中的#shop1 并将其附加到index.html DOM 中。

我知道这样的傻事:

$('#toshop2').on('click', function() {
    window.open('shop.html#shop2', '_parent');
});

会起作用(是的,不会有过渡)。

问题是(假设没有其他解决方案,只能破解):

  1. 我可以/应该以不同的方式破解它吗?
  2. 我还能以某种方式过渡到外部页面吗?

【问题讨论】:

    标签: javascript jquery-mobile navigation


    【解决方案1】:

    您可以手动请求外部文档,然后只将您想要的部分插入 DOM:

    //delegate event handler binding to links with a custom class
    //(any link that links to a pseudo-page in an external document
    $(document).delegate('.my-external-links', 'click', function () {
    
        //get the location of the external document and the requested page (hash)
        var theHREF   = this.href,
            theTarget = '#shop1';
        if (theHREF.indexOf('#') > -1) {
            theTarget = '#' + theHREF.split('#')[1];
            theHREF   = theHREF.split('#')[0];
        }
    
        //first see if this page is already in the DOM, if so just navigate to it
        if ($(theTarget).length) {
            $.mobile.changePage($(theTarget));
            return;
        }
    
        //show the loading spinner
        $.mobile.showPageLoadingMsg();
    
        //do AJAX request to get new pages into the DOM
        $.ajax({
            url : theHREF,
            success : function (response) {
    
                //hide the loading spinner
                $.mobile.hidePageLoadingMsg();
    
                //find all the `data-role="page"` elements and add them to the DOM
                $(response).find('[data-role="page"]').appendTo('body');
    
                //now navigate to the requested page which should be in the DOM
                $.mobile.changePage($(theTarget));
            },
            error   : function (jqXHR, textStatus, errorThrown) { /*don't forget to handle errors*/ }
        });
        return false;
    });
    

    我很确定这也有一个插件,但正如您所见,它并没有那么多代码或复杂性。

    【讨论】:

    • 我可能还不清楚。我的意思是我不想将外部 HTML 页面插入 DOM。我想以编程方式模拟相当于&lt;a href="shop.html#shop2" rel="external"&gt;&lt;/a&gt;
    • 我就是这么做的。首先,您从外部文档中获取所有 data-role="page" 元素,然后将它们添加到当前 DOM(您必须这样做,因为 jQuery Mobile 将忽略文档中除第一个伪页面之外的所有内容),然后您可以导航到他们。这一切都只需通过向您想要展示此行为的链接添加一个类来完成。基本上,您可以从外部文档中提取您想要的内容,但您也可以加载所有页面,这样您就不必创建另一个 HTTP 请求。请参阅关于 DOM 中已有页面的更新
    • 我不得不说,我不太明白为什么您需要将多个伪页面放入外部文档中。为什么不为每个页面创建一个外部文档?
    • 因为每个外部 HTML 代表一个小应用程序,有自己的 JS,多个内页等等。我不想页面。我想加载整个 HTML 文件。我知道这可能不是“JQM 方式”,但我只是要求“更漂亮的 hack”。你描述的AFAIK并没有模拟我提到的rel=external"
    • 如果您这样做,您将无法利用页面动画。您必须进行整页刷新(如果您不想使用我提供的解决方案)。为什么不将每个伪页面放在它自己的文档中,然后将这些部分拆分为文件夹?您可以使用包含来引用全局或部分范围的资产。
    【解决方案2】:

    为什么不尝试使用两次 changepage?

    $.mobile.changePage( "shop.html", { transition: "none"} );
    $.mobile.changePage( "#shop2", { transition: "slideup"} );
    

    或者只是使用 loadPage 在后台将 shop.html 加载到 DOM 中,然后更改为#shop2?

    $.mobile.loadPage( "shop.html" );
    $.mobile.changePage( "#shop2", { transition: "slideup"} );
    

    【讨论】:

    • 我会尝试并告诉你。我怀疑我至少必须断开hashChange 的连接(作为changePage 的参数)。
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多