pagecontainerbefore仅更改为特定页面

时间:2017-08-07 16:22:43

标签: jquery jquery-mobile

如何仅将pagecontainerbeforechange实现到特定页面

我有像这样的jquery

$(document).on("pagecontainerbeforechange", function(e, data) {

  alert(data.toPage[0].id); //TO TEST Name of PAGE

  if (data.toPage[0].id == "detailsPage") // I DID this, but it didn't work
  {
    alert("DetailsPage");
    //Do Ajax
  }
});

1 个答案:

答案 0 :(得分:-2)

试试这个:

$(document).on("pagecontainerbeforechange", function (e, data) {
    alert(data.toPage[0].id); //TO TEST Name of PAGE
    if(data.toPage[0].id == "detailsPage") {
        alert ("DetailsPage");
        //Do Ajax
    }
});

示例:



$(document).on("pagecontainerbeforechange", function (e, data) {
    e.preventDefault();
    //alert(data.toPage[0].id); //TO TEST Name of PAGE
    if(data.toPage[0].id == "detailsPage") {
        console.log("DetailsPage");
        //Do Ajax
    }
});

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.js"></script>
  <script type="text/javascript" src="//code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
  <link rel="stylesheet" type="text/css" href="//code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
</head>
<body>

  <div data-role="page" id="detailsPage" data-pagename='First Page'>
    <div data-role="content">
        <div class='trace'> <a href="#second" data-role="button" data-mini='true' data-inline='true'>Second Page</a>

        </div>
    </div>
</div>
&#13;
&#13;
&#13;