我是一个JS初学者,我想,如果在列表上有超过1个标记,则显示text1 else显示text2
这是基本代码
function makeSidebar() {
var html = "";
for (var i=0; i<gmarkers.length; i++) {
if (gmarkers[i].getVisible()) {
html += '<div class="pin-details col-xs-3" onClick="javascript:myclick(' + i + ')" ><div class="col-xs-2"><img src="map/pin_grey.png"/></div><div class="col-xs-10"><span>'+gmarkers[i].myname+'</span><p>ul. '+gmarkers[i].myaddress+'<br>'+gmarkers[i].myaddresstwo+'</div></div>';
}
}
document.getElementById("side_bar").innerHTML = html;
}
我试图像这样做
for (var i=0; i<gmarkers.length; i++) {
var markersVisible = gmarkers[i].getVisible();
if (markersVisible.length == 1) {
if (markersVisible) {
html += '<div class="pin-details col-xs-3" onClick="javascript:myclick(' + i + ')" ><div class="col-xs-2"><img class="img1" alt="Map Pin" src="map/pin_grey.png"/></div><div class="col-xs-10"><span>'+gmarkers[i].myname+'</span><p>ul. '+gmarkers[i].myaddress+'<br>'+gmarkers[i].myaddresstwo+'</div></div>';
}
}
else{
if (markersVisible){
html += '<div class="pin-details col-xs-3" onClick="javascript:myclick(' + i + ')" ><div class="col-xs-2"><img class="img1" alt="Map Pin" src="map/marker_red.png"/></div><div class="col-xs-10"><span>'+gmarkers[i].myname+'</span><p>ul. '+gmarkers[i].myaddress+'<br>'+gmarkers[i].myaddresstwo+'</div></div>';
}
}
答案 0 :(得分:0)
你忘了写你的问题,所以我不明白你的问题,但这里有一些变化,更少的行
for (var i=0; i<gmarkers.length; i++)
{
var markersVisible = gmarkers[i].getVisible();
if (markersVisible)
html += '<div class="pin-details col-xs-3" onClick="javascript:myclick(' + i + ')" ><div class="col-xs-2"><img class="img1" alt="Map Pin" src="map/'+(markersVisible.length == 1 ? "pin_grey" : "marker_red")+'.png"/></div><div class="col-xs-10"><span>'+gmarkers[i].myname+'</span><p>ul. '+gmarkers[i].myaddress+'<br>'+gmarkers[i].myaddresstwo+'</div></div>';"col-xs-10"><span>'+gmarkers[i].myname+'</span><p>ul. '+gmarkers[i].myaddress+'<br>'+gmarkers[i].myaddresstwo+'</div></div>';
}