jQuery mobile - 获取加载的页面ID?

Bil*_*lie 1 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>
Run Code Online (Sandbox Code Playgroud)

它会警告"未经分类"而不是页面ID.

问题是什么?

小智 8

请注意,jQuery Mobile 1.4中不推荐使用pageshow:http://api.jquerymobile.com/pageshow/

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

如果要获取活动页面ID,可以执行以下操作:

$.mobile.pageContainer.pagecontainer( 'getActivePage' ).attr( 'id' );
Run Code Online (Sandbox Code Playgroud)

要么

$('body').pagecontainer( 'getActivePage' ).attr( 'id' );
Run Code Online (Sandbox Code Playgroud)

要么

$(':mobile-pagecontainer').pagecontainer( 'getActivePage' ).attr( 'id' );
Run Code Online (Sandbox Code Playgroud)

我个人不推荐后两种,因为你可以自由设置一个不同的页面容器,实际上我在Jasmine测试中做了.

请注意,页面必须首先处于活动状态(例如,它在pagecreate中尚未激活).