我有一个地图标记网站,可以从Google地图中抓取Tattoo Parlors,并将它们作为标记添加到我们的交互式地图中。
我有一个带有“Messenger字段”的数据库。基本上,一些标记上有一个“发送消息”按钮标记,用户可以在其中直接发送消息。
但是,并非所有标记都需要此标记,因此如果数据返回NULL,我希望隐藏标记。
这是代码:
//assign an infowindow to the marker so that when its clicked it shows the name of the place
google.maps.event.addListener(marker, 'click', (function (marker, x) {
return function () {
infowindow.setContent("<div class='no-scroll'><strong>" + results[x]['place']['name'] + "</strong><br><br>"
+ results[x]['place']['address'] + "<br><br>"
+ "<span style=\"display:table;margin:0 auto; color: #e7711b;\">"
+ results[x]['place']['rating'] + " " + WPacStars.rating_render(results[x]['place']['rating'], 16, 'e7711b')
+ "</span>"
+"<div id='more_info' align='center' style='margin: 0 auto;'>"
+ "<a href='" + WPURLS.siteurl + "/reviews?place_id=" + results[x]['place']['id']
+ "' target='_blank' style='text-align: center; display: block; margin-bottom: 5px; margin-top: 5px;text-decoration: none; font-size: 18px; color: #6495ed'>click here for more info</a>"
+ "<a class="messenger-link" href='" + results[x]['place']['messenger']
+ "' target='_blank' style='text-align: center; display: block; text-decoration: none; font-size: 14px; text-decoration: none; color: #6495ed'>Send Message</a>"
+ "</div></div>");
infowindow.open(MYMAP.map, marker);
}
})(marker, x));
}
}
}
);
}
//}}
希望有人可以帮忙/给予指导!
干杯
您可以使用setAttribute在事后有条件地添加该属性。如果您有数据,请添加属性。
删除所有没有a
设置的href
标签
function removeAnchors(){
$("a").each(function(){
if($(this).attr("href") == "" || $(this).attr("href") == "null" || $(this).attr("href") == undefined || $(this).attr("href") == null) {
$(this).remove();
}
})
}
removeAnchors(); // Call this function after adding the html dynamically
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="example.com">Send Button</a>
<a href="">Send Button</a>
<a href="">Send Button</a>
<a href="">Send Button</a>
<a href="">Send Button</a>
您可以使用以下代码替换JS代码。它可能对你有帮助。
google.maps.event.addListener(marker, 'click', (function (marker, x) {
return function () {
var infoContent = "<div class='no-scroll'><strong>" + results[x]['place']['name'] + "</strong><br><br>";
infoContent += results[x]['place']['address'] + "<br><br>";
infoContent += "<span style=\"display:table;margin:0 auto; color: #e7711b;\">";
infoContent += "results[x]['place']['rating'] + " " + WPacStars.rating_render(results[x]['place']['rating'], 16, 'e7711b')</span>";
infoContent += "<div id='more_info' align='center' style='margin: 0 auto;'>";
infoContent += "<a href='" + WPURLS.siteurl + "/reviews?place_id=" + results[x]['place']['id'] +" target='_blank' style='text-align: center; display: block; margin-bottom: 5px; margin-top: 5px;text-decoration: none; font-size: 18px; color: #6495ed'>click here for more info</a>";
// this condition should be change as per your requirement as I dont know which data you want to compare with NULL value
if(results[x]['place']['messenger'] != '' && results[x]['place']['messenger'] != null){
infoContent += "<a class="messenger-link" href='" + results[x]['place']['messenger'] + "' target='_blank' style='text-align: center; display: block; text-decoration: none; font-size: 14px; text-decoration: none; color: #6495ed'>Send Message</a>";
}
infoContent += "</div></div>";
infowindow.setContent(infoContent);
infowindow.open(MYMAP.map, marker);
}
})(marker, x));