将样式应用于函数循环中生成的li [英] Applying style to li generated in loop of function
本文介绍了将样式应用于函数循环中生成的li的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@nrabinowitz
@nrabinowitz
我现在想知道如何在单击以将样式应用于上一节中创建的LI时获取地图图标.那可能吗?我在下面的代码中尝试过,但是没有用.
I am now wondering how to get the map icon when clicked to apply a style to the LI that is created in the previous section. Is that possible? I attempted below in the code but does not work.
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);
推荐答案
您在li
和#I
之间留有空格.这实际上意味着,请给我一个元素,它是li的后代,且ID为I
.删除空格以将其更正为id为I
的li:
You have a space between li
and #I
. That effectively means, get me the element that is a descendant of an li and has an id of I
. Remove the space to correct it to be the li with an id of I
:
$("#leadsList li#"+I).css({"background-color":"#666666"});
但是您仍然不需要大部分的选择器.只需引用ID-除非有特殊情况.
But you don't need most of that selector anyway. Just reference the id - unless you have a special case.
$("#"+I).css({"background-color":"#666666"});
这篇关于将样式应用于函数循环中生成的li的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文