Javascript 如何仅在单击特定条件时启用功能
大家好,我有3个按钮,分别是1.ADD、2.EDIT、3.DELETE……和一个id=comp_map的地图。。。我正在使用开放式街道地图Javascript 如何仅在单击特定条件时启用功能,javascript,jquery,Javascript,Jquery,大家好,我有3个按钮,分别是1.ADD、2.EDIT、3.DELETE……和一个id=comp_map的地图。。。我正在使用开放式街道地图 function addComp() { $("#comp_map").click(function() { if (event.type !== 'mousemove') { var containerPoint = comp_map.mouseEventToContainerPoint(ev
function addComp() {
$("#comp_map").click(function() {
if (event.type !== 'mousemove') {
var containerPoint = comp_map.mouseEventToContainerPoint(event),
layerPoint = comp_map.containerPointToLayerPoint(containerPoint),
latlng = comp_map.layerPointToLatLng(layerPoint)
alert("Marker Location: "+latlng);
}
});
}
function editComp() {
// disable the map click
}
function delComp() {
// disable the map click
}
我的问题是我想要
$(“#comp_map”)。单击仅在单击“添加”按钮时工作。。。但是,当其他按钮,如编辑,删除被单击此功能应该不工作。。。这是正确的方法吗?如果我的方法是错误的,请纠正我。。。谢谢你 因此,您需要跟踪应用程序/按钮的状态,以便在单击地图时,可以根据该状态以不同方式处理交互:
在你的JS中
$(function() {
//set default action to add. If no default set action = false
var action = 'add';
//Bind to button clicks and update stored state
$('body').on('click', 'button', function(e){
var newAction = $(e.target).data('action');
if ($(e.target).data('action')) {
action = newAction;
}
});
//bind to map clicks and handle based on current action state
$("#comp_map").click(function(e) {
//you might want this conditional in your addComp() fn depending on what you need in editComp()/delComp()
if (e.type !== 'mousemove') {
e.preventDefault();
switch (action) {
case "add":
addComp(e);
break;
case "edit":
editComp(e);
break;
case "delete":
delComp(e);
break;
default:
return false
break;
}
}
})
function addComp(e) {
var containerPoint = comp_map.mouseEventToContainerPoint(event),
layerPoint = comp_map.containerPointToLayerPoint(containerPoint),
latlng = comp_map.layerPointToLatLng(layerPoint)
alert("Marker Location: "+latlng);
}
function editComp(e) {
// disable the map click
}
function delComp(e) {
// disable the map click
}
});
然后在HTML中为要选择的操作设置数据属性(您也可以在单击时设置selected
类以显示当前操作:
<button data-action="add">Add</button>
<button data-action="edit">Edit</button>
<button data-action="delete">Delete</button>
添加
编辑
删除
因此,根据最近单击/激活的按钮,单击地图应该调用不同的功能?或者我误解了您的意图吗?