我希望第一个功能haritaOne
在按下按钮时起作用,再次按下时如何运行第二个功能haritaTwo
?
function haritaOne() {
var harita = document.getElementById('map-canvas');
harita.style.left = "0px";
}
function haritaTwo() {
var harita = document.getElementById('map-canvas');
const trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
harita.style.left = "0px";
}
<a href="#" onclick="javascript: haritaOne();"</a>
使用跟踪状态的变量使其成为一个函数
var yourState = false
function yourFunction() {
var harita = document.getElementById('map-canvas');
harita.style.left = "0px";
if (yourState) {
const trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);
}
yourState = !yourState;
}
var yourState = false
function yourFunction() {
console.log('yourFunction called');
if (yourState) {
console.log('In True');
} else {
console.log('In False');
}
yourState = !yourState;
}
<button type="button" onclick="yourFunction()">Click</button>
现在我认为您正在尝试切换叠加层,这意味着您需要实际删除它,而不仅仅是调用一些随机函数
var trafficState = null;
var trafficOverlay = null;
function haritaOne() {
// create overlay if it has yet to be created
if (!trafficOverlay) {
trafficOverlay = new google.maps.TrafficLayer();
}
// get the new state of the overlay
trafficState = trafficState ? null : map;
//set the overlay
trafficOverlay.setMap(trafficState);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句