【问题标题】:how to get attributes of element current pointed by mouse如何获取鼠标指向的当前元素的属性
【发布时间】:2013-12-17 22:30:09
【问题描述】:

我正在使用 d3.js 并使用它创建了 svg,我在其上绘制了地图和圆圈。在这些圆圈之上,我绘制了隐形圆圈,以便我可以悬停在隐形圆圈上。现在,当我将鼠标悬停在它们上方时,我想通过调用 on 事件函数来进行一些转换。在此 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

当我将鼠标悬停在不可见圆圈上时,我正在对该特定圆圈进行转换,因为我需要访问该不可见圆圈的属性。

这里是代码

        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 r=d3.select(this).attr("cx");
                        d3.select(this).attr("stroke-width",0)
                     .style('fill-opacity','0')
                     .attr("r",radius);});

    }

    }

【问题讨论】:

  • 你已经在使用 "d3.select(this).attr("cx");" 访问鼠标指向的圆的属性了

标签: javascript d3.js


【解决方案1】:

在事件处理程序中,this 指的是当前元素。所以你可以通过d3.select(this).attr(...) 来获取你想要的任何属性的值。

【讨论】:

  • 是的,我想通了。
猜你喜欢
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 2016-06-16
  • 1970-01-01
相关资源
最近更新 更多