将样式应用于在函数循环中生成的li
@nrabinowitz
我现在想知道如何在单击时将地图图标应用于上一节中创建的LI。 那可能吗? 我在下面尝试了代码,但不起作用。
function(I) { // within the scope of this function, // "thismarker" will work as expected var listid = I + 1; $("") .html('' + listid + '' + '' + data[I][3] + ' ' + data[I][4] + '' + data[I][2] + '') .click(function(){ infowindow.close(); infowindow.setContent(contentString[I]); map.setCenter(markers[I].position); infowindow.open(map, markers[I]); }) .appendTo("#leadsList ul"); google.maps.event.addListener(marker, "click", function(e){ infowindow.close(); infowindow.setContent(contentString[I]); infowindow.open(map, markers[I]); $("#leadsList li #"+I).css({"background-color":"#666666"}); }); })(I);
任何dom元素的id都不能是整数,它应该始终以alpha或特殊字符开头。 试试这个
function(I) { // within the scope of this function, // "thismarker" will work as expected var listid = I + 1; $("") .html('' + listid + '' + '' + data[I][3] + ' ' + data[I][4] + '' + data[I][2] + '') .click(function(){ infowindow.close(); infowindow.setContent(contentString[I]); map.setCenter(markers[I].position); infowindow.open(map, markers[I]); }) .appendTo("#leadsList ul"); google.maps.event.addListener(marker, "click", function(e){ infowindow.close(); infowindow.setContent(contentString[I]); infowindow.open(map, markers[I]); $("#leadsList li #_"+I).css({"background-color":"#666666"}); }); })(I);
li
和#I
之间有空格。 这实际上意味着,让我得到一个李的后代元素,并具有I
的身份。 删除空格以将其更正为ID为I
的li:
$("#leadsList li#"+I).css({"background-color":"#666666"});
但是你无论如何都不需要大部分选择器。 只需引用id – 除非你有特殊情况。
$("#"+I).css({"background-color":"#666666"});
你在某些选择器中缺少引号。
而不是$("
") ,请尝试: $('
') 。