2011-06-14 62 views
1

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

在此先感谢。

$('#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"); 

     }); 

回答

2

这是错词? $('#DateListView李“)应该有匹配的单或双引号

此:

$('#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 

UPDATE:

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

您好,感谢菲尔,,我试过。但它显示的错误$(本).setAttribute是不确定的。请帮我 – Finder 2011-06-15 04:14:36

+0

而不是setAttribute使用attr,看我的UPDATE – 2011-06-15 12:45:58