【问题标题】:Jquery mobile losing formatting on a ul listviewJquery mobile在ul listview上丢失格式
【发布时间】:2013-07-25 17:55:33
【问题描述】:

这是我的列表视图元素:

<ul data-role="listview" data-inset="true" id="acceptedContent" data-theme="c">
</ul>

创建过程这第一次运行良好,列表视图看起来像它应该的那样并且似乎工作得很好:

$('#acceptedContent').append('<li><a href="#Info' + results.rows.item(i).infoID + '" data-transition="flow">' + results.rows.item(i).Info + '</a></li>');

然后我调用它来清空 div,它可以工作并清除 div:

$('#acceptedContent').empty();

使用 ajax 帖子刷新页面,当我取回数据时,我将其读入数据库,然后重建页面。此列表是唯一丢失格式的页面。然后我调用第一次构建页面时使用的相同函数。

重建部分 数据已填充,但我失去了格式

$('#acceptedContent').append('<li><a href="#Info' + results.rows.item(i).infoID + '" data-transition="flow">' + results.rows.item(i).Info + '</a></li>');

任何建议都将不胜感激,因为我一直在尝试解决这个问题。

我已经尝试了以下操作以及执行顺序,但没有任何效果,我在重建部分进行附加调用后将以下列表视图刷新:

$("#acceptedContent").listview('refresh');
$("#acceptedContent ul").listview('refresh');

感谢您的帮助。

【问题讨论】:

  • 嗨!你能在这里发布你的JS代码吗?我的意思是您添加列表等的整个工作流程

标签: jquery jquery-ui jquery-mobile


【解决方案1】:

好的,这就是你的做法:

你有一个这样的列表视图:

<ul data-role="listview" data-inset="true" id="acceptedContent" data-theme="c">
</ul>

您有一些数据,您将循环并格式化为&lt;li/&gt;。您可以将li html 收集到一个变量中,而不是多次附加,然后只附加一次:

var li = "";
//your loop, $.each, for or for..in
li += '<li><a href="#Info' + results.rows.item(i).infoID + '" data-transition="flow">' + results.rows.item(i).Info + '</a></li>';
//end of loop

所以现在,您已将所有数据放在一个名为 li 的变量中。现在你将如何使用它?放到&lt;ul/&gt;

$('#acceptedContent').html(li);

这样可以减少emptyappend 的使用。只需使用html() 擦除和重写。

现在是重要的部分。您必须刷新列表视图。你必须使用listview("refresh"),但不是你使用的方式。您必须等待 html() 完成其工作。只有这样,你必须使用refresh api,像这样:

$('#acceptedContent').html(li).promise().done(function () {
   //refresh here - $(this) refers to ul here
   $(this).listview("refresh");
});

那里有一些按钮。以防万一您可以使用 trigger 方法刷新它们。所以你的代码看起来像这样:

$('#acceptedContent').html(li).promise().done(function () {
   //refresh here - $(this) refers to ul here
   $(this).listview("refresh");
   //causes a refresh to happen on the elements such as button etc. WHICH lie inside ul
   $(this).trigger("create");
});

注意: 如果您收到此错误

未捕获的错误:无法在 listview 之前调用方法 初始化;试图调用方法“刷新”。

改变

$(this).listview("refresh");

$(this).listview().listview("refresh");

【讨论】:

  • 我收到以下错误:07-25 14:28:49.598: D/CordovaLog(9579): Uncaught Error: cannot call methods on listview before initialization;试图调用方法“刷新”。
  • 废话,现在我得到 Uncaught TypeError: Cannot read property 'jQuery191008186558447778225' of undefined
  • 从好的方面来说,我确实发现了一些谷歌在“'jQuery191008186558447778225”上没有的东西,记下时间和日期。
  • 你能解决你的问题吗?
  • 很高兴能帮上忙 :)
【解决方案2】:

试试下面的代码:

$('div[data-role=page]').page('destroy').page();

我是这样使用的:

$("someid").ajaxStop(function(){
 $('div[data-role=page]').page('destroy').page();
});

这很好地重建了我的 css。

【讨论】:

  • 页面方法已被弃用,哥们。 (除非 OP 使用的是古老的 jQM 版本)。您必须使用 trigger("create") 刷新
  • @hungerpain 你是对的,我还没有意识到,是的,我正在运行旧版本的 jQuery Mobile。谢谢。
猜你喜欢
  • 2013-06-30
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多