2011-07-31 25 views
0

迴路中產生@nrabinowitz應用樣式李在功能

我現在想點擊一個樣式應用到在上一節中創建的LI時如何獲得地圖圖標。那可能嗎?我在代碼的下面嘗試,但不起作用。

function(I) { 
    // within the scope of this function, 
    // "thismarker" will work as expected 
    var listid = I + 1; 
    $("<li id="+I+"/>") 
     .html('<span class="leadId">' + listid + '</span>' + '<div class="leadDataWrapper"><div class="leadName">' + data[I][3] + ' ' + data[I][4] + '</div><div class="leadDate">' + data[I][2] + '</div></div><div class="leadType"><img src="/images/map-' + data[I][1].toLowerCase() + '.png"></div>') 
     .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); 

回答

0

您在某些選擇器中缺少了引號。

而不是$("<li id="+I+"/>"),請嘗試:$('<li id="' + I + '" />')

1

任何dom元素的id不能是整數,它應該始終以alpha或特殊字符開頭。試試這個

function(I) { 
    // within the scope of this function, 
    // "thismarker" will work as expected 
    var listid = I + 1; 
    $("<li id='_"+I+"'/>") 
     .html('<span class="leadId">' + listid + '</span>' + '<div class="leadDataWrapper"><div class="leadName">' + data[I][3] + ' ' + data[I][4] + '</div><div class="leadDate">' + data[I][2] + '</div></div><div class="leadType"><img src="/images/map-' + data[I][1].toLowerCase() + '.png"></div>') 
     .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); 
1

你有li#I之間的空間。這實際上意味着,請將我的元素作爲李的後代,並且ID爲I。刪除空間來糾正它是與I一個id李:

$("#leadsList li#"+I).css({"background-color":"#666666"}); 

但你並不需要大多數選擇的反正。只需引用id--除非你有特殊情況。

$("#"+I).css({"background-color":"#666666"});