jQuery mobile不会在页面之间触发页面显示代码

时间:2016-04-26 01:23:35

标签: javascript jquery jquery-mobile

我正在编写一个包含两个HTML页面的jQuery移动应用程序。当用户从index.html页面导航到search.html页面时,jQuery应该将元素加载到search.html页面。

然而,事情似乎出了问题。该脚本无法按预期工作。

的index.html

<!DOCTYPE html>
<head>
<!-- Include meta tag to ensure proper rendering and touch zooming -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<!-- Include jQuery Mobile stylesheets -->
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" ></link>
<link rel="stylesheet" href="css/mahidol.min.css"></link>
<link rel="stylesheet" href="css/themes/jquery.mobile.icons.min.css"></link>
<!-- Include the jQuery library -->
<script src="js/jquery-1.11.3.min.js"></script>
<!-- Include the jQuery Mobile library -->
<script src="js/jquery.mobile-1.4.5.min.js"></script>
<!-- Cordova sh*ts -->
<script type="text/javascript" src="cordova.js"></script>
<script src="js/parameterHandler.js"></script>
<script src="js/default.js"></script>
</head>
<body>
  <div data-role="page" id="index">
    <div data-role="header" data-position="fixed">
      <a href="prefs.html" class="ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-gear">Preferences</a>
      <h1><img src="logo.png" height="15px">&nbsp;Contact</h1>
    </div>
    <div data-role="main" class="ui-content">
      <form class="ui-filterable">
        <a href="search.html" class="ui-btn" data-transition="flip">Search from name</a>
        <input id="filterBoxList" data-type="search" placeholder="Type here to search from department list">
      </form>
      <ul data-role="listview" data-filter="true" data-input="#filterBoxList" id="list_dept">
      </ul>
    </div>
  </div>
</body>

search.html

<!DOCTYPE html>
<head>
<!-- Include meta tag to ensure proper rendering and touch zooming -->
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<meta charset="utf-8"></meta>
<!-- Include jQuery Mobile stylesheets -->
<link rel="stylesheet" href="css/jquery.mobile-1.4.5.min.css" ></link>
<link rel="stylesheet" href="css/mahidol.min.css"></link>
<link rel="stylesheet" href="css/themes/jquery.mobile.icons.min.css"></link>
<!-- Include the jQuery library -->
<script src="js/jquery-1.11.3.min.js"></script>
<!-- Include the jQuery Mobile library -->
<script src="js/jquery.mobile-1.4.5.min.js"></script>
<!-- Cordova shits -->
<script type="text/javascript" src="cordova.js"></script>
<script src="js/parameterHandler.js"></script>
<script src="js/default.js"></script>
<style>
  #listie small{
    color: #ccc !important;
  }
</style>
</head>

<body>
  <div data-role="page" id="search">
    <div data-role="header" data-position="fixed">
      <a href="prefs.html" class="ui-btn ui-shadow ui-corner-all ui-btn-icon-left ui-icon-gear">Preferences</a>
      <h1><img src="logo.png" height="15px">&nbsp;Contact</h1>
    </div>
    <div data-role="main" class="ui-content">
      <form class="ui-filterable">
        <a href="#index" class="ui-btn" data-transition="flip">Search from dept. list</a>
        <input id="filterBoxName" data-type="search" placeholder="Type here to search from name">
      </form>
      <ul data-role="listview" data-filter="true" data-input="#filterBoxName" id="listie">
      </ul>
    </div>
  </div>
</body>

default.js(加载最后一个)

$(document).on("pageshow","search.html",function(){
  var chx = 0;
  var m="";
  var k=[];
  $.mobile.loading( "show", {
          text: 'Loading',
          textVisible: true
  });
  if(loaded == false) // This will prevent event triggering more then once
  {
    for (var t in JSON.parse(localStorage.getItem('hr')))
    {
      m += "<li><a href='#view?id=" + t + "'>" + JSON.parse(localStorage.getItem('hr'))[t].Name + " <small>" + JSON.parse(localStorage.getItem('hr'))[t].Surname + "</small></a></li>";
    }
    $('#listie').append(m).listview('refresh');
    loaded = true;
  }
  $.mobile.loading( "hide" );
});

当“搜索”页面div放在index.html中时,事情似乎很好(并且代码链接在单个页面中,而不是两个)。但是,当我尝试将文件拆分为两个文件时,它确实会中断。

我能做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:0)

好吧,解决了......

即使div在同一个文件上,当使用$(document).on("pageshow",src,function(){...});指向src时,请确保src指向div ID,而不是文件本身。

在这种情况下,它是$(document).on("pageshow","#search",function(){...});