我想访问当前元素的父元素
这是HTML的结构
svg
g id=invisibleG
g
circle
g
circle
g
circle
Run Code Online (Sandbox Code Playgroud)
基本上我想在我将鼠标悬停在圈子上时在圈子中添加文字.
所以我希望在任何特定圈子的悬停上都有这样的东西
svg
g id=invisibleG
g
circle --> radius is increased and text presented inside that
text
g
circle
g
circle
Run Code Online (Sandbox Code Playgroud)
在悬停时我可以通过d3.select(this)选择当前元素,如何获得根元素(在我的情况下为g)?
我正在使用d3.js并使用我创建的svg,我已经绘制了地图和圆圈.在这些圈子之上,我画了一个看不见的圆圈,这样我就可以在不可见的圆圈上盘旋.现在,当我将鼠标悬停在它们上面时,我想通过调用on事件函数来进行一些转换.在事件函数中,我希望得到我正在悬停的圆的属性.
这是HTML页面的结构
table
tbody
tr
td
svg
rect (boundary of canvass)
g id=outerG
g
path
g
path
g
circle
g
circle
g id=invisibleCircle
g
circle cx,cy,r,text --> I will hover over this circle & access attributes
g
circle cx,cy,r,text
Run Code Online (Sandbox Code Playgroud)
当我将鼠标悬停在不可见的圆圈上时,我正在对该特定圆圈进行过渡,因为我需要访问该不可见圆圈的属性.
这是代码
var radius=some logic to calculate radius
//Main circle d3.select("body").select("svg").select("#outerG").insert("g","#invisibleG").append("circle")
.attr("cx",coords[0])
.attr("cy",coords[1])
.attr("r",radius)
.attr("class","circle")
.attr("id",weekData[q].bob[p].city+"circle")
.style('fill', 'tan');
//Invisible circle d3.select("body").select("svg").select("#outerG").select("#invisibleG").append("g").append("circle")
.attr("cx",coords[0])
.attr("cy",coords[1])
.attr("r",radius)
.attr("class","inviCircle")
.attr("id",weekData[q].bob[p].city+"invisible")
.style('fill-opacity', '0')
.on("mouseover",function(){
var r=d3.select(this).attr("cx");
d3.select(this).style('fill','tan').style('fill-opacity', '1').transition()
.duration(1000)
.attr("r",r) ;
d3.select(this).attr("stroke","blue").attr("stroke-width",4);
})
.on("mouseout",function(){
// var …Run Code Online (Sandbox Code Playgroud)