“back"按钮转到目的地,然后返回原始页面
"back" button goes to destination then back to original page
我正在使用Phonegap和jQuery开发一个应用程序。
我有个小毛病,把我难住了。
我有一个后退按钮,它看起来像这样:
<div data-role="header">
<a id="backFromModules" data-role="button" data-icon="back">Back</a>
<h1 id="mHeader" class="wrap"></h1>
</div>
和使按钮导航回#home
页面的javascript:
$(document).on("click", "#backFromModules", function(event){
console.log("back from modules");
$.mobile.changePage("#home");
});
当我点击后退按钮时,它导航到"#home",然后回到模块页面,但这只是间歇性地随机发生。
这个视频应该更好地解释它:https://youtu.be/CyQz0KiNC64
我不知道如何调试这个,或者什么可能导致错误。如果有人有任何想法,请告诉我
编辑
本页的完整代码可以在这里找到:
<div id="modules" data-role="page">
<div data-role="header">
<a id="backFromModules" data-role="button" data-icon="back">Back</a>
<h1 id="mHeader" class="wrap"></h1>
</div>
<div data-role="content">
<p>Below is a list of e-learning modules which can be viewed for this course</p>
<ul id="mList" data-role="listview" data-inset="true">
<li>An error has occurred!</li>
</ul>
</div>
<div data-role="footer" data-position="fixed" class="ui-grid-b">
<div class="ui-block-a">
<p>©Copyright information</p>
</div>
<div class="ui-block-b">
</div>
<div class="ui-block-c">
<div class="ui-btn-right" data-role="controlgroup" data-type="horizontal">
<a href="#about" data-transition="slideup" data-role="button" data-iconpos="notext" data-icon="info" >About</a>
</div>
</div>
</div>
</div>
和主页看起来像这样:
<div id="home" data-role="page" >
<div data-role="header">
<h1 class="wrap">Qlearn App Contents</h1>
</div>
<div data-role="content">
<p>Courses:</p>
<ul id="cList" data-role="listview" data-inset="true"> <li>An error has occurred!</li>
</ul>
</div>
<div data-role="footer" data-position="fixed" class="ui-grid-b">
<div class="ui-block-a">
<p>Qlearn||build: 1.0.8</p>
</div>
<div class="ui-block-b">
</div>
<div class="ui-block-c">
<div class="ui-btn-right" data-role="controlgroup" data-type="horizontal">
<a href="#about" data-transition="slideup" data-role="button" data-iconpos="notext" data-icon="info" >About</a>
</div>
</div>
</div>
</div>
和我有一些javascript记录每一个更改页面事件
$(document).on("pagechange", function(event){
console.log("Page Change");
console.log(event);
});
下面是click处理程序和click处理程序调用的函数的代码:
$(document).on("click", "#cList li a", function(event) {
loadModules(this.text);
return false;
});
function loadModules(course){
console.log("Starting mod retrieval")
var data = {
"fn" : "mod",
"data": course,
"ajax" : "true"
};
$.ajax({
type: "GET",
url: SERVICE_URL,
data: data,
contentType: "application/json",
success: function(response) {
console.log("Success!");
console.log(response);
var i, list = "";
if($.isArray(response.module)){
for (i = 0; i < response.module.length; i++) {
console.log(response.module[i]);
list += formatModuleListItem(response.module[i]);
$.mobile.changePage("#modules");
$("#mList").html(list).listview().listview('refresh');
}
}else{
list = formatModuleListItem(response.module);
$.mobile.changePage("#modules");
$("#mList").html(list).listview().listview('refresh');
}
}
});
console.log("end of mod retrieval")
}
更新我已经改变了上面的代码,以显示我所做的一些更改,以尝试和调试这个问题。这表明,有时(但并非总是)当我单击主页上动态创建的一个按钮时,changepage事件会触发两次。
只有当事件触发两次时才会发生此错误,这表明当我导航回主页时,该事件的第二次发生正在排队并触发。
所以我想知道是否有人知道是否有某种形式的队列,存储可以清除的更改事件?
好了,经过很长时间的消除过程,我意识到只有当modules
页面上的列表中有多个模块时才会发生错误。
这意味着我的问题在:
if($.isArray(response.module)){
for (i = 0; i < response.module.length; i++) {
console.log(response.module[i]);
list += formatModuleListItem(response.module[i]);
$.mobile.changePage("#modules");
$("#mList").html(list).listview().listview('refresh');
}
}
一旦我可以分解它,问题变得非常明显!
所以,为了其他可能遇到这个问题的人的利益:
行$.mobile.changePage("#modules")
在for
循环中,这意味着每次循环到达该行时都会调用它。
的解决方案是移动
$.mobile.changePage("#modules");
$("#mList").html(list).listview().listview('refresh');
退出循环。
感谢大家的帮助
相关文章:
- 将元素移动到右侧,然后返回到左侧
- 如何点击标题中的链接,然后返回HTML、Javascript和CSS
- 如何比较两个数组,然后返回差异的索引
- 使用函数for循环遍历对象以更改值,然后返回结果
- Javascript:编写一个函数,接收一个数组,然后返回一个只有唯一数字的数组,只删除数组
- AngularJS工厂调用PHP,然后返回AngularJS
- 从mysql数据库中删除项目,然后返回最后一页
- 每次我点击我的网站,它都会重定向到home.html,然后返回到我点击的所需url
- 修改输入中的数组,然后返回输出
- 如何将 $http.put 封装到一个函数中,该函数执行某些操作,然后返回通过或失败的承诺
- Javascript 的提示,它接受简单的数学作为值,然后返回答案
- 铁:路由器工作很奇怪 - 将我发送到模板然后返回
- 在 中显示警告框 asp.net 将重定向到空白页,然后返回到原始页面
- 导演.js和涡轮链接 - 更改窗口哈希并使用然后返回
- Javascript函数,它接受可变数量的参数 - 更改它们的值 - 然后返回它们
- 我可以将字段保留在填充的表单中,即使我移动到其他页面然后返回
- javascript重定向到带有表单操作post的页面,然后返回到带有输入值的上一页
- 从垂直滚动到水平滚动,然后返回到垂直滚动
- 使用isNan()函数将参数从html传递给javascript,并检查值然后返回值
- 循环通过LI's在一个顺序中,然后返回另一个顺序,同时逐渐消失