console
d3.select(svg).append("circle")
.attr("cx", 100)
.attr("cy", 100)
.attr("r", 50)
.style("fill", "lightblue");
var tooltip = d3.select('body').append('div')
.style('position', 'absolute')
.style('padding', '8px')
.style('background', '#f0f0f0')
.style('border', '1px solid #808080')
tooltip.style('opacity', 0);
d3.select(svg).selectAll('circle').on('mouseenter', function() {
var circle = d3.select(this);
var cx = +circle.attr("cx");
var cy = +circle.attr("cy");
tooltip.html("<strong>圆心:</strong>(" + cx + ", " + cy + ")" + '<br><a href="#" id="tipLink">查看详细信息</a>');
tooltip.transition()
.duration(200)
.style("opacity", 1.0);
document.getElementById('tipLink').addEventListener('click', function(e) {
e.preventDefault();
alert('您点击了链接!');
});
})
.on('mousemove', function() {
tooltip.style(
'left',
d3.event.pageX - (tooltip.node().offsetWidth /2) +"px"
)
tooltip.style(
'top',
d3.event.pageY - (tooltip.node().offsetHeight+20)+"px"
)
})
.on(`mouseleave`, function(){
tooltip.transition()
.duration(300)
.style(`opacity`, 0);
});
<svg id="svg" width="400" height="800" />