Jquerymobile列表视图

Jquerymobile ListView

本文关键字:视图 列表 Jquerymobile      更新时间:2023-09-26

我正在使用以下代码动态创建一个列表。它工作正常,但当我单击特定的列表项时,所选行的字体颜色应该变为黄色。我该怎么做?

提前谢谢。

$('#DateListView').children().remove('li');
        //Make a new list
        var parent = document.getElementById('DateListView');
        for (var menuid = 0; menuid < weekStartDates.length; menuid++) {
            var listItem = document.createElement('li');
            listItem.setAttribute('id', 'listitem_' + weekStartDates[menuid]);
            listItem.innerHTML = "<div data-role='button' style='margin-left:10px;font-size:15px'data-theme ='c'  id='" + menuId + "'>" + Hai +"</div>";
            parent.appendChild(listItem);
        }
        var list = document.getElementById('DateListView');
        $(list).listview("refresh");
        $('#DateListView li ").bind("click", function() {
            $(this).setAttribute("style" , "font-color:yellow");
       });

这是打字错误吗$('#DateListView li")应具有匹配的单引号或双引号

此:

$('#DateListView li ").bind("click", function() {
    $(this).setAttribute("style" , "font-color:yellow");
});

应为:

$('#DateListView li').bind("click", function() {
    $(this).setAttribute("style" , "font-color:yellow");
});

或:

$("#DateListView li").bind("click", function() {
    $(this).setAttribute("style" , "font-color:yellow");
});

此外,您可能希望在添加标记后调用刷新

$("#DateListView li").bind("click", function() {
    $(this).setAttribute("style" , "font-color:yellow");
});
$(list).listview("refresh"); // Move after added markup

更新:

 $("#DateListView li").bind("click", function() {
    $(this).attr("style" , "font-color:yellow");
});
$(list).listview("refresh"); // Move after added markup