【问题标题】:jQuery Mobile: Collapsible not expanded in pageinit, when coming back from other pagejQuery Mobile:从其他页面返回时,在 pageinit 中未展开可折叠
【发布时间】:2011-10-21 06:08:07
【问题描述】:

在我的 jquery-mobile (1.0RC2) 应用程序中,我有两个页面:test1.html、test2.html

第一页 test1.html 包含一个可协作集,我在其中通过 pageinit 事件侦听器中的脚本展开一个可协作项(已尝试 pageshow 和 pageinit):

$('#page1').live( 'pageinit', initPage);
function initPage() {
    alert('initPage!');  // this line seems to be always getting executed
    $('#my_expandable').trigger('expand');  // ... but this line doesn't when coming back via a back link!
}

这在页面的第一次调用时工作正常。

然后我有一个链接,把我引到第二页test2.html,如下:

<script>
function goPage2(criteria) {
    $('#page1').die( 'pageinit', initPage);
    $.mobile.changePage( "test2.html", {reverse: false, reloadPage: true} );
}
</script>

<a href="javascript:goPage2('brand');" rel="external" class="ui-link-inherit" style="margin-top:0px;padding-top:0px;margin-bottom:0px;padding-bottom:0px;">page 2</a>

当我然后通过

返回第一页时
<script>
function goPage1() {
    $.mobile.changePage( "test1.html", { reverse: true, reloadPage: true} );
}
</script>
<a href="javascript:goPage1();" rel="external" class="ui-link-inherit">test</a>

只执行了test1.html的pageinit事件监听器中的alert消息,但是collapsible没有通过

展开

您可以在此处查看示例:http://bit.ly/rr0dq3

如何重现问题:

  • http://bit.ly/rr0dq3加载test1.html
  • 您将收到一条警报消息,并且折叠栏将展开
  • 点击“GoTo page2”按钮,您将来到第二页test2.html
  • 在第二页上,单击灰色按钮“test”,您将返回第一页 test1.html
  • 现在的问题:如您所见,test1.html 的 pageinit 事件的警报命令正在执行,但展开可折叠不是 - 为什么不呢?显然pageinit事件监听方法输入正确,但是这里似乎只有可折叠的有问题。

我认为这可能是一个错误(在此处提交报告 https://github.com/jquery/jquery-mobile/issues/2791),但也许其他人对此有想法。

解决方法: 当我使用不同的方式打开第二页 test2.html 时,警报和可折叠扩展都在执行,使用

window.location.href = "test2.html";

而不是

changePage(...);

但这并不是很令人满意。如果我使用页面注入方式,为什么它不能正常工作?当我打开不同的页面时,我已经调用了 die() 方法,以免有多个 pageinit 事件监听器一直徘徊。

【问题讨论】:

    标签: jquery-mobile


    【解决方案1】:

    您是否尝试过将data-attribute 用于可折叠的内容区域以使其加载扩展:

    data-collapsed="false"
    

    这里是此行为的文档链接:http://jquerymobile.com/demos/1.0rc2/docs/content/content-collapsible.html

    【讨论】:

    • 我知道该属性,但这不符合我的要求。我需要通过脚本动态展开或折叠项目,而不是通过属性固定。当然,我的示例非常基础,但是在我的实际应用程序中,我有多个可折叠项,并且根据某些条件,当用户返回页面时,我需要展开/折叠一些,有些则不需要。
    • 我发现有效的是 $.mobile.changePage("test1.html",{changeHash: true});但是网址显示不正确
    猜你喜欢
    • 2012-01-14
    • 2013-07-11
    • 1970-01-01
    • 2018-08-21
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    相关资源
    最近更新 更多