更改页面后未触发页面显示 [英] Pageshow not triggered after changepage

查看:14
本文介绍了更改页面后未触发页面显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery Mobile,我想在用户单击主页上的按钮后将浏览器重定向到另一个页面.为此,我写道:

$.mobile.changePage("album-search-results.html",{数据:{区域:搜索区域,值:搜索值},过渡:流行"});

而且它工作正常,它加载了正确的页面,甚至在 URL 上放置了正确的值.不幸的是,pageshow 事件没有被触发:

 <头><meta name="viewport" content="width=device-width, initial-scale=1"><script src="http://code.jquery.com/jquery-1.8.2.min.js"></script><script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script><link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css"/><身体><div data-role = "page" data-theme = "d" id = "SearchResults"><div data-role = "header"><h1>聚合器</h1>

<div data-role = "content">

<script type="text/javascript">$("#SearchResults").on("pageshow",function(event, ui){控制台日志(ui.prevPage);});

当我从上一个页面加载此页面时,控制台始终为空.怎么了?谢谢

解决方案

解决方案

解决方案很简单,在页面 div 内移动脚本块.在您的情况下,脚本块被丢弃.基本上像这样改变它:

来自:

<div data-role = "page" data-theme = "d" id = "SearchResults"><div data-role = "header"><h1>聚合器</h1>

<div data-role = "content">

<script type="text/javascript">$("#SearchResults").on("pageshow",function(event, ui){控制台日志(ui.prevPage);});

致:

<div data-role="page" data-theme="d" id="SearchResults"><div data-role = "header"><h1>聚合器</h1>

<div data-role = "content">

<脚本>$(document).on("pageshow","#SearchResults",function(event, ui){控制台日志(ui.prevPage);});

示例:

index.html

<头><title>jQM Complex Demo</title><meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/><link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css"/><script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script><script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script><脚本>$(document).on('click', '#change-page', function(){$.mobile.changePage("专辑搜索结果.html",{数据:{区域:asda",值:1"},过渡:流行"});});<身体><div data-role="page" id="index"><div data-theme="a" data-role="header"><h3>第一页<a href="#second" class="ui-btn-right">下一步</a>

<div data-role="内容"><a data-role="button" id="change-page">更改页面</a>

<div data-theme="a" data-role="footer" data-position="fixed">

album-search-results.html

<头><meta name="viewport" content="width=device-width, initial-scale=1"><script src="http://code.jquery.com/jquery-1.8.2.min.js"></script><script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script><link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css"/><身体><div data-role="page" data-theme="d" id="SearchResults"><div data-role = "header"><h1>聚合器</h1>

<div data-role = "content">

<脚本>$(document).on("pageshow","#SearchResults",function(event, ui){控制台日志(ui.prevPage);});

I am using jQuery Mobile and I would like to redirect the browser to another page, after the user clicked on a button on the home. To do this I wrote:

$.mobile.changePage("album-search-results.html",{
               data:{area:searchArea, value:searchValue},
               transition: "pop"
           });

And it works fine, it loads the correct page and it even puts the right values on the URL. Unfortunately the pageshow event is not triggered:

    <!DOCTYPE html>
<html>
<head>


    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
</head>

<body>
    <div data-role = "page" data-theme = "d" id = "SearchResults">

        <div data-role = "header">
            <h1>Aggregator</h1>
        </div>

        <div data-role = "content">

        </div>
    </div>

    <script type="text/javascript">
        $("#SearchResults").on("pageshow",function(event, ui){
            console.log(ui.prevPage);

        });

    </script>
</body>
</html>

The console in always empty when I load this page from the previous one. What is wrong? Thanks

解决方案

Solution

Solution is simple, move script block inside a page div. In your case script block is discarded. Basically change it like this:

From :

<body>
    <div data-role = "page" data-theme = "d" id = "SearchResults">

        <div data-role = "header">
            <h1>Aggregator</h1>
        </div>

        <div data-role = "content">

        </div>
    </div>

    <script type="text/javascript">
        $("#SearchResults").on("pageshow",function(event, ui){
            console.log(ui.prevPage);

        });

    </script>
</body>

To :

<body>
    <div data-role="page" data-theme="d" id="SearchResults">
        <div data-role = "header">
            <h1>Aggregator</h1>
        </div>
        <div data-role = "content">

        </div>
        <script>
            $(document).on("pageshow","#SearchResults",function(event, ui){
                console.log(ui.prevPage);
            });
        </script>       
    </div>
</body>

Example:

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>      
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>    
        <script>
            $(document).on('click', '#change-page', function(){       
                $.mobile.changePage("album-search-results.html",{
                    data:{area:"asda", value:"1"},
                    transition: "pop"
                });
            });                 
        </script>
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="a" data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>

            <div data-role="content">
                <a data-role="button" id="change-page">Change Page</a>
            </div>

            <div data-theme="a" data-role="footer" data-position="fixed">

            </div>
        </div>    
    </body>
</html>   

album-search-results.html

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
</head>
    <body>
        <div data-role="page" data-theme="d" id="SearchResults">
            <div data-role = "header">
                <h1>Aggregator</h1>
            </div>
            <div data-role = "content">

            </div>
            <script>
                $(document).on("pageshow","#SearchResults",function(event, ui){
                    console.log(ui.prevPage);
                });
            </script>       
        </div>
    </body>
</html>

这篇关于更改页面后未触发页面显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆