我有美国所有县的 SVG 渲染图。每条路径都绑定了四个事件:
click : opens up tooltip graphic with three buttons
dblclick : executes zoom effect
mouseover : shows tooltip with county/state data, changes fill
mouseout : returns fill to original
我的问题是我想区分使用按钮打开工具提示的地图的单击事件和该工具提示内的按钮。
我通过创建工具提示
thisObj._tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
并且地图是通过创建的
d3.json("/static/js/json/us-counties.json", function(json){
thisObj._svg.selectAll("path")
.data(json.features)
.enter().append("path")
.attr("d", thisObj._path)
.attr("class", "states")
.attr("id", function(d){
return d.id;
})
.style("fill", "gray")
.style("stroke", "black")
.style("stroke-width", "0.5px")
.on("click", mapClick)
.on("dblclick", mapZoom)
.on("mouseover", mapMouseOver)
.on("mouseout", mapMouseOut);
});
我的 mapClick 处理程序是
var mapClick = function(d, i){
if (thisObj._tooltipOpen){
if ($(".button").is(":checked")){
console.log($(this).attr("id") + " was clicked");
} else {
console.log("Close tooltip click recorded");
thisObj._tooltip.transition()
.duration(500)
.style("opacity", 0);
thisObj._tooltipOpen = false;
mapMouseOut(d);
}
} else {
console.log("Open tooltip click recorded");
//tooltip template in html, want to handle a button click on these buttons
var template = "<div id = 'tooltip_template'>" +
"<div class = 'county_data'>" + d.name + ", " + d.properties.StateCode + "</div>" +
"<button id = 'add_prospective_market' class = 'button'>Prospective Market</button>" +
"<button id = 'add_market' class = 'button'>Market County</button>" +
"<button id = 'remove_market' class = 'button'>Remove Market</button></div>";
thisObj._tooltip.html(template)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY + "px"));
$(".button").button();
thisObj._tooltip.transition()
.duration(1000)
.style("opacity", .8);
thisObj._tooltipOpen = true;
}
}
我有一个通过实例化的按钮处理程序
$("#add_market").on("click", function(){
console.log("Add Market button clicked");
}
但它从未被激活,我得到的唯一日志是工具提示打开/关闭通知。鉴于此代码,我如何修改单击处理程序以区分按钮单击和非按钮单击?