【问题标题】:jQuery mobile data-rel="back" link causing wrong events to be firedjQuery mobile data-rel="back" 链接导致错误事件被触发
【发布时间】:2011-10-20 12:36:08
【问题描述】:

看看test-case

当您打开链接时,第 1 页pagebeforeshow 被触发。当您单击链接转到 page 2 时,会触发 page 2pagebeforeshow。到目前为止,一切顺利。

如果您随后使用左键 (data-rel="back") 返回,则会触发多余的事件。改用右键(直接链接到 page 1)可以达到我的预期,即只有 page 1pagebeforeshow 被触发。

pagebeforeshow也可以换成pageshow,没关系。这里发生了什么?

(在最新的 Chrome 中测试)

参考来源:

<html>
  <head>
    <meta charset="utf-8">

    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script>

    <script>
      $('#test1').live('pagebeforeshow', function() {
          console.log("=== pagebeforeshow for #test1");
      });
      $('#test2').live('pagebeforeshow', function() {
          console.log("=== pagebeforeshow for #test2");
      });
    </script>
  </head>

  <body>
    <div data-role="page" id="test1">

      <div data-role="header" align="center">
        <p>Page 1.</p>
      </div><!-- /header -->

      <div data-role="content">
        <p><a href="#test2">Go to page 2.</a></p>
      </div><!-- /content -->

    </div><!-- /page -->

    <div data-role="page" id="test2">

      <div data-role="header" align="center">
        <a href="/" data-icon="back" data-rel="back">Back</a>
        <p>Page 2.</p>
        <a href="data-rel-back.html" data-icon="back">Go directly to page 1</a>
      </div><!-- /header -->

      <div data-role="content">
        <p>
        Try the two buttons and have a look at the console.<br>
        Using the left button (data-rel="back") triggers "too many" events.<br>
        The right button does what I'd expect.
        </p>
      </div><!-- /content -->

    </div><!-- /page -->
  </body>
</html>

【问题讨论】:

标签: javascript jquery mobile jquery-mobile jquery-events


【解决方案1】:

请更改链接以重定向到第 1 页

<a href="data-rel-back.html" data-icon="back">Go directly to page 1</a>

应该是

<a href="/#test1" data-icon="back">Go directly to page 1</a>

如果您发现此删除正斜杠有任何问题

 <a href="#test1" data-icon="back">Go directly to page 1</a>

【讨论】:

  • 在原始版本中,我有两个 html 页面设置。我只是在这个例子中合并了它们。这就是我链接到.html 的原因。在此示例中不应有所作为,因为 JQM 会自动获取文件中的第一页。
【解决方案2】:

直播Example

更正了页眉和后退按钮属性中的标签

<a data-rel="back">Back</a>

jQM 在渲染页面时添加了额外的标记,如果你使用了错误的标记,一些功能可能会改变,中断。我认为后退按钮发生的事情是您还添加了 href="/" 属性,这可能导致 jQM 尝试导航到它以及返回导致问题的最后一页。

同样在您使用&lt;p&gt; 标记并将其居中对齐的标题中,jQM 在使用&lt;h1&gt; 时会这样做

有关后退按钮的更多信息,您可以随时参考docs

【讨论】:

  • JQM 文档明确提到您还应该为(出于某种原因)不支持后退功能的客户端定义“href”属性。除此之外,对我而言,您的代码更改仍会产生与以前完全相同的结果。在原始应用程序中,中心有一个图像,这就是我不使用

    .

    的原因
【解决方案3】:

原来,这是 Chrome 控制台中的 bug

bugtracker

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 2013-03-26
    • 2012-05-21
    • 2012-10-09
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多