2013-10-16 64 views
2

我在JavaScript的吸这么难 - 我无法弄清楚如何改变列表项的背景色在jQuery的列表视图...jQuery的列表视图改变列表项的颜色

var parent = document.getElementById('listDiv'); 

var listview = document.createElement('ul'); 
listview.setAttribute('id', 'listView'); 

parent.appendChild(listview); 
var listItem = document.createElement('li'); 
listItem.setAttribute('id', 'temp'); 
var itemLink = document.createElement('a'); 
itemLink.setAttribute('href', '#'); 
itemLink.innerHTML = "HEYY MAN"; 

listItem.appendChild(itemLink); 
listview.appendChild(listItem); 


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

//I NEED DIS SHIT TO WORK 
$('#listView').on('click', 'li', function(){ 
    $('#temp').css('background-color','green'); 
}); 

一切都是正确显示,并且您可以在JSFiddle上玩弄它,但“点击”功能或背景颜色更改无法正常工作。有人可以伸出援手吗?

+0

你的问题内容实际上是我的问题的解决方案:) –

回答

2

您正在使用的jQuery版本(1.6.4)不支持.on()函数,它仅在版本1.7中引入。我和.bind()取代它让它暂时的工作:

$('#listView').bind('click', 'li', function(){ 
    $('#temp').css('background','green'); 
}); 

background-color改为只background以覆盖jQuery Mobile的造型。

这是工作:http://jsfiddle.net/q3vfh/3/

但是,如果你不依赖于该版本的jQuery那么我建议使用此更新的小提琴代码:http://jsfiddle.net/q3vfh/2/ - 它用了jQuery 2.0.2和jQuery移动1.3.1,并允许您使用.on()

+0

谢谢你,我花了太长时间试图找出问题。我没有被绑定到那个版本,所以我会做出开关! – kburbach

1
$('#listView').delegate('li','click', function(){ 
$(this).css({background: '#6495ED'}) 
}); 
相关问题