简体   繁体   中英

jquery show hide the requested page

Problem is to show/hide requested specific page from among pages in the html. The code I tried is shown below. Can any one point me the right direction. I n the blow code I tried to show only pagethree, but it displayed pageone.

<!DOCTYPE html PUBLIC >
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link rel="stylesheet" href="jquery.mobile-1.2.0.min.css" /> 
<script src="jquery-1.8.1.min.js"></script>
<script src="jquery.mobile-1.2.0.min.js"></script>   

<script>
$("#pageone").hide();
$("#pagetwo").hide();
$("#pagethree").show();
</script>

</head>

<body>

<div data-role="page" id="pageone">
  <div data-role="header">
    <h1>page one</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>page one contents </p>
  </div>

  <div data-role="footer">
    <h1>page one</h1>
  </div>
</div> 

<div data-role="page" id="pagetwo">
  <div data-role="header">
    <h1>page two</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>page two contents</p>
  </div>

  <div data-role="footer">
    <h1>page two</h1>
  </div>
</div> 



<div data-role="page" id="pagethree">
  <div data-role="header">
    <h1>page three</h1>
  </div>

  <div data-role="main" class="ui-content">
    <p>page three contents...</p>
  </div>

  <div data-role="footer">
    <h1>page three</h1>
  </div>
</div> 



</body>
</html>

To prevent page from showing or redirect a user before next page is shown, you need to listen to pagebeforechange event. This event fires before changing page transition is commenced and URL History is updated.

When pagebeforechange is emitted, it omits a data object holding details of previous and next page. Use this data to determine from and which pages the user is navigating.

$(document).on("pagebeforechange", function (e, data) {
    var nextPage = data.toPage,
        prevPage = data.options.fromPage;
    if (typeof nextPage === "object" && typeof prevPage === "undefined") {
        var page = nextPage[0].id;
        if (page === "pageone") {
            $.mobile.changePage("#pagethree", {
                transition: "flip"
            });
            return false;
        }
    }
});

Using .show() or .hide() will get you nowhere. In addition, refrain from using .ready() or $(function () {}); in jQuery Mobile and use Page Events instead.

Demo

Try to put your code inside pagecreate event:

Triggered when the page has been created in the DOM (via ajax or other) and after all widgets have had an opportunity to enhance the contained markup.

<script>
$(document).on('pagecreate', function() {
    $("#pageone").hide();
    $("#pagetwo").hide();
    $("#pagethree").show();
});
</script>
<script>
$(document).ready(function()
{
$("#pageone").hide();
$("#pagetwo").hide();
$("#pagethree").show();
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM