Javascript 更改页面后未触发页面显示

Javascript 更改页面后未触发页面显示,javascript,html,jquery-mobile,jquery,Javascript,Html,Jquery Mobile,Jquery,我正在使用jQuery Mobile,在用户单击主页上的按钮后,我想将浏览器重定向到另一个页面。 为此,我写道: $.mobile.changePage("album-search-results.html",{ data:{area:searchArea, value:searchValue}, transition: "pop" }); 它工作正常,加载正确的页面,甚至在URL上输入正确的值。很遗憾,page

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

$.mobile.changePage("album-search-results.html",{
               data:{area:searchArea, value:searchValue},
               transition: "pop"
           });
它工作正常,加载正确的页面,甚至在URL上输入正确的值。很遗憾,pageshow事件未触发:

    <!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>

聚合器
$(“#搜索结果”)。在(“页面显示”,函数(事件,用户界面){
console.log(ui.prevPage);
});
当我从上一页加载此页时,中的控制台始终为空。怎么了? 谢谢

解决方案 解决方案很简单,将脚本块移动到页面div中。在这种情况下,脚本块将被丢弃。基本上是这样改变的:

发件人:

<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>
<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>

聚合器
$(“#搜索结果”)。在(“页面显示”,函数(事件,用户界面){
console.log(ui.prevPage);
});
至:

<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>
<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>

聚合器
$(文档)。在(“页面显示”,“搜索结果”,函数(事件,用户界面){
console.log(ui.prevPage);
});
例子: 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>   
<!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>

jQM复杂演示
$(文档)。在('单击','更改页面',函数(){
$.mobile.changePage(“相册搜索结果.html”{
数据:{区域:“asda”,值:“1”},
过渡:“流行音乐”
});
});                 
首页
换页
相册搜索结果.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>   
<!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>

聚合器
$(文档)。在(“页面显示”,“搜索结果”,函数(事件,用户界面){
console.log(ui.prevPage);
});
解决方案 解决方案很简单,将脚本块移动到页面div中。在这种情况下,脚本块将被丢弃。基本上是这样改变的:

发件人:

<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>
<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>

聚合器
$(“#搜索结果”)。在(“页面显示”,函数(事件,用户界面){
console.log(ui.prevPage);
});
至:

<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>
<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>

聚合器
$(文档)。在(“页面显示”,“搜索结果”,函数(事件,用户界面){
console.log(ui.prevPage);
});
例子: 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>   
<!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>

jQM复杂演示
$(文档)。在('单击','更改页面',函数(){
$.mobile.changePage(“相册搜索结果.html”{
数据:{区域:“asda”,值:“1”},
过渡:“流行音乐”
});
});                 
首页
换页
相册搜索结果.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>   
<!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>

聚合器
$(文档)。在(“页面显示”,“搜索结果”,函数(事件,用户界面){
console.log(ui.prevPage);
});

我以为pageshow只是目标文档的一个事件?我以为pageshow只是目标文档的活动?