javascript - jQuery Mobile - 获取加载的页面 ID?

标签 javascript jquery jquery-mobile

我有一个名为“info.html”的文档。

它有 5 页:

  1. 食物
  2. 玩具
  3. 娱乐
  4. 智能手机
  5. 电子

我希望在页面加载后,jquery 向服务器发送请求以获取有关此项目的信息。

我都需要知道的是如何触发这个功能?

尝试过:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>CoCouppn!</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
        <script type="text/javascript">
            $(document).on("pageshow", function() {
                alert($(this).attr('id'));
            });
        </script>
    </head>
    <body>
        <div data-role="page" class="pge" id="food">
            food
        </div>
        <div data-role="page" class="pge" id="toys">
            toys
        </div>
        <div data-role="page" class="pge" id="entertainment">
            entertainment
        </div>
        <div data-role="page" class="pge" id="smartphones">
            smartphones
        </div>
        <div data-role="page" clsas="pge" id="electronics">
            electronics
        </div>

    </body>
</html>

它会提示“未定义”而不是页面 ID。

问题是什么?

最佳答案

请注意 pageshow 在 jQuery Mobile 1.4 中已弃用:http://api.jquerymobile.com/pageshow/

另请注意 $.mobile.activePage 已弃用。

如果你想获取事件页面ID,你可以这样做:

$.mobile.pageContainer.pagecontainer( 'getActivePage' ).attr( 'id' );

$('body').pagecontainer( 'getActivePage' ).attr( 'id' );

$(':mobile-pagecontainer').pagecontainer( 'getActivePage' ).attr( 'id' );

我个人不推荐后两者,因为您可以自由设置不同的 pagecontainer,事实上我在 Jasmine 测试中就是这样做的。

请注意,该页面必须首先真正处于事件状态(例如,它在 pagecreate 中尚未处于事件状态)。

关于javascript - jQuery Mobile - 获取加载的页面 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20760643/

相关文章:

javascript - 如何使用javascript选择下拉选项并更改其值

javascript - 在 Chrome 上的 Web Serial API 中获取连接设备的名称

jquery - 如何使用 authlogic 和 jQuery 验证插件验证密码?

javascript - div里面的按钮不显示

javascript - 保持下拉菜单可见,直到使用 Jquery 在其外部单击

jQuery 在 taphold 事件后调用 click 事件

javascript - 如果选中复选框,如何自动粘贴值

javascript - 为什么 >=(大于或等于)比较在 Javascript 中不起作用?

php - CSS + jQ 插件阻止 AJAX 调用/事件监听器工作?

css - 网络浏览器中的移动 "push"页面 - 怎么样?