Javascript 使用jQuery mobile版本1.0rc3刷新listview
我正在开发jquery.mobile-1.0rc3版本的平板电脑应用程序。以前,我在另一个应用程序上使用了jquery.mobile-1.0a4.1版本,可以通过执行myListview.listview(“刷新”)来刷新listview 我在使用新的jquery.mobile-1.0rc3版本时也遇到了一些问题。使用新的jquery.mobile-1.0rc3版本可以做到这一点吗 多谢各位 下面是一些代码:Javascript 使用jQuery mobile版本1.0rc3刷新listview,javascript,jquery,listview,mobile,jquery-mobile,Javascript,Jquery,Listview,Mobile,Jquery Mobile,我正在开发jquery.mobile-1.0rc3版本的平板电脑应用程序。以前,我在另一个应用程序上使用了jquery.mobile-1.0a4.1版本,可以通过执行myListview.listview(“刷新”)来刷新listview 我在使用新的jquery.mobile-1.0rc3版本时也遇到了一些问题。使用新的jquery.mobile-1.0rc3版本可以做到这一点吗 多谢各位 下面是一些代码: var lists = $( '#posicaoIntegradaActivosLis
var lists = $( '#posicaoIntegradaActivosList, #posicaoIntegradaPassivosList, #posicaoIntegradaOutrosList' );
lists.empty();
/* Fill the lists with jquery template */
lists.listview( "refresh" );
错误:
未捕获异常:无法在之前调用listview上的方法
初始化;试图调用方法“刷新”
根据代码运行的时间,它可能在jQuery Mobile初始化过程之前运行。默认情况下,jsiddle在
load
事件触发后运行代码,因此DOM已全部设置完毕,jQuery Mobile已完成初始化。如果您将@Phill Pafford的jsFiddle()更改为在“no wrap(body)”而不是“onLoad”上运行,那么您将得到与报告相同的错误。因此,我建议删除lists.listview('refresh')代码>行或将代码放入文档中。就绪
或页面显示/页面创建
事件处理程序:
var lists = $( '#posicaoIntegradaActivosList, #posicaoIntegradaPassivosList, #posicaoIntegradaOutrosList' );
lists.empty();
/* Fill the lists with jquery template */
//lists.listview( "refresh" );
下面是一个JSFIDLE,用于在浏览器解析代码后立即运行代码:
或:
下面是一个JSFIDLE,用于将代码包装到文档中。ready
事件处理程序:
或:
下面是一个用于使用pageshow
事件的JSFIDLE:
下面是一个用于使用pagecreate
事件的JSFIDLE:
另请注意:如果要检测jQuery Mobile是否已初始化某个元素,可以在该元素上检查jQuery Mobile特定的类:
$(function () {
//cache lists
var lists = $( '#posicaoIntegradaActivosList, #posicaoIntegradaPassivosList, #posicaoIntegradaOutrosList' );
//iterate through the lists
lists.each(function (index, value) {
//cache this specific list
var $value = $(value);
/*add rows to this listview here*/
//check if the listview has been initialized by jQuery Mobile by checking for the existence of the `ui-listview` class
if ($value.hasClass('ui-listview')) {
//since the class was found, refresh the already initialized element
$value.listview('refresh');
} else {
//the class was not found, so initialize the widget
$value.trigger('create');
}
});
});
您的示例代码的工作方式与我在这里测试的一样:并且无误地获得了预期的结果。您正在运行哪个版本的jQuery?RC3要求1.6.4您能提供HTML代码吗?如果您执行lists.listview();列表前。列表视图(“刷新”);Phill,您的示例逻辑与我的代码相同,并且工作正常。我真的不明白这里出了什么问题,我正在使用jQuery 1.6.4;但它给了我另一个错误!这是不必要的,因为listview已经在HTML中初始化了。您能为您的代码提供一个工作演示吗?或者给我们一个URL来测试?这将有助于调试。从A4.1版到RC3版,很多东西都发生了变化,被重新分解,重新命名,等等……贾斯珀,我遵循了你的逻辑,它成功了。问题是,在我尝试刷新listview时,小部件甚至没有创建,因此代码最终类似于:$(lists).each(function(){($(this.hasClass('ui listview'))?$(this.listview('refresh'):$(this.trigger('create');});非常感谢你!
$('#my-page-id').on('pagecreate', function () {
var lists = $( '#posicaoIntegradaActivosList, #posicaoIntegradaPassivosList, #posicaoIntegradaOutrosList' );
lists.empty();
/* Fill the lists with jquery template */
//lists.listview( "refresh" );
}
$(function () {
//cache lists
var lists = $( '#posicaoIntegradaActivosList, #posicaoIntegradaPassivosList, #posicaoIntegradaOutrosList' );
//iterate through the lists
lists.each(function (index, value) {
//cache this specific list
var $value = $(value);
/*add rows to this listview here*/
//check if the listview has been initialized by jQuery Mobile by checking for the existence of the `ui-listview` class
if ($value.hasClass('ui-listview')) {
//since the class was found, refresh the already initialized element
$value.listview('refresh');
} else {
//the class was not found, so initialize the widget
$value.trigger('create');
}
});
});