[英]Jquery Mobile: How to style dynamically added list items when listview() function does not work?
我是jQuery Mobile和jQuery的新手,遇到了問題。 我已經使用2個AJAX調用填充了列表(來自兩個來源的數據)。 第二個調用位於第一個調用中,因此它始終在它之后執行。 除了數據源之外,這些調用是相同的,如下所示:
HTML:
<ul data-role="listview" data-inset="true" data-theme="a" data-filter="true" id="allVideosList">
<li><a href="#">This item is styled properly</a></li>
</ul>
JavaScript的:
var html = "";
var id = 0;
var displayName = "name";
$.ajax({
type:'GET',
url: xxxx
data: xxxx
dataType: 'jsonp',
success: function processResult(data) {
....
html += '<li data-filtertext="' + resultData.name + ' ' + date + '"><a href="index.html?id=' + id + '">' + displayName + '</a></li>';
$('#allVideosList').append(html);
});
變量在ajax調用中定義,但此處省略。 創建的列表沒有任何樣式,每個項目都顯示為鏈接。 功能:
$('#allVideosList').listview('refresh');
要么
$('#allVideosList').listview();
返回錯誤
$(...).listview
不是函數
在控制台中。 功能:
$('#allVideosList').trigger('create');
不會改變任何東西。
如果我將變量html打印到控制台,復制其內容,然后將其添加到其他頁面的列表中,則樣式將正確應用。 例如:
HTML:
<div data-role="content" id="list">
<ul data-role="listview" data-inset="true" data-theme="a" data-filter="true" id="allVideosList">
</ul>
JAVASCRIPT:
var html = "";
html = '<li data-filtertext="insert text here"><a href="index.html?id=146">Name</a></li><li data-filtertext="insert text here 2"><a href="index.html?id=147">Name 2</a></li>'
$("#allVideosList").append(html)
工作正常。 如何將樣式應用於動態加載的列表?
我在一個項目中做過非常相似的事情。 我遇到了類似的麻煩,但這是當前對我有用的代碼...
$("#myList").append(myjsonobject.someListItems).listview("refresh");
如您所見,您可以在添加調用的同一行中添加listview函數調用。 沒有更多的代碼,很難說這是否可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.