简体   繁体   English

Jquery Mobile:当listview()函数不起作用时,如何为动态添加的列表项设置样式?

[英]Jquery Mobile: How to style dynamically added list items when listview() function does not work?

I am new with jQuery Mobile and with jQuery in general and have run into a problem. 我是jQuery Mobile和jQuery的新手,遇到了问题。 I have been populated a list using 2 AJAX calls (data from two sources). 我已经使用2个AJAX调用填充了列表(来自两个来源的数据)。 The second call is located within the first so that it always executed after it. 第二个调用位于第一个调用中,因此它始终在它之后执行。 The calls are identical apart from the source of data and are as follows: 除了数据源之外,这些调用是相同的,如下所示:

HTML: 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: 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);
});

Variables are defined within the ajax call but omitted here. 变量在ajax调用中定义,但此处省略。 The list created lacks any style, each item appears as a link. 创建的列表没有任何样式,每个项目都显示为链接。 The function: 功能:

$('#allVideosList').listview('refresh');

or 要么

$('#allVideosList').listview();

returns the error 返回错误

$(...).listview is not a function $(...).listview不是函数

in the console. 在控制台中。 The function: 功能:

$('#allVideosList').trigger('create');

does not change anything. 不会改变任何东西。

If I print the variable html to the console, copy its contents, and add this to a list in a different page, style is applied properly. 如果我将变量html打印到控制台,复制其内容,然后将其添加到其他页面的列表中,则样式将正确应用。 For example: 例如:

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) 

works fine. 工作正常。 How can I apply a style to the dynamically loaded list? 如何将样式应用于动态加载的列表?

I do something very similar in one of my projects. 我在一个项目中做过非常相似的事情。 I had similar trouble but this is the code that currently works for me... 我遇到了类似的麻烦,但这是当前对我有用的代码...

$("#myList").append(myjsonobject.someListItems).listview("refresh");

As you can see, you can add the listview function call in the same line as your append call. 如您所见,您可以在添加调用的同一行中添加listview函数调用。 Without more of your code, it's hard to tell if this will resolve your issue. 没有更多的代码,很难说这是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态添加ListView时jQuery Mobile页脚未修复 - jQuery Mobile footer not fixed when listview dynamically added 滚动不能动态添加项目到jQuery移动列表 - Scrolling not working on dynamically added items to jQuery mobile list 为什么 ::before CSS Selector 在动态添加项目时不适用于列表项目? - Why does ::before CSS Selector not work with list items when a item is added dynamically? 添加项目时“Jquery Mobile”复选框列表不会更新,并且“刷新”脚本以对其进行样式设置 - Jquery Mobile checkbox-list does not update when adding items and “refreshing” the script to style it 动态将项目添加到列表视图(jQuery mobile) - Dynamically adding items to listview (jQuery mobile) 如何计算使用jQuery动态添加到页面的列表项的数量? - How to count the number of list items that are dynamically added to a page using jQuery? jQuery hasClass在检查动态添加的类时不起作用 - jquery hasClass does't work when checking dynamically added class 如何告诉jquery mobile将类添加到listview中动态添加的表单字段? - How do I tell jquery mobile to add classes to dynamically added form fields in a listview? jQuery动态添加行时如何使用自动完成功能 - jQuery How to use Autocomplete function when rows are added dynamically jQuery Mobile动态扩展的ListView不起作用 - jquery mobile dynamically extended listview doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM