如何从D3按钮调用javascript函数

如何从D3按钮调用javascript函数,javascript,d3.js,svg,Javascript,D3.js,Svg,我使用以下代码在D3SVG弹出窗口中创建按钮 window.div = d3.select("body").append("div") .attr("class", "tooltip") .style("opacity", 0); sampleSVG.append('circle') .attr('id', 'endcricle'+idendelement++) .style("stroke", "black") .style("stroke-wi

我使用以下代码在D3SVG弹出窗口中创建按钮

 window.div = d3.select("body").append("div") 
    .attr("class", "tooltip")       
    .style("opacity", 0);

sampleSVG.append('circle')
  .attr('id', 'endcricle'+idendelement++)
  .style("stroke", "black")
  .style("stroke-width", "4")
  .style("fill", "white")
  .attr('transform', 'translate(' + d3.event.pageX + ',' + d3.event.pageY+  ')')
  .attr('r', '20')
  .on("mouseover", function(){
     d3.select(this).style("fill", "aliceblue");
     var t = d3.select(this).attr("id");
        function getScreenCoords(x, y, ctm) {
          var xn = ctm.e + x*ctm.a + y*ctm.c;
          var yn = ctm.f + x*ctm.b + y*ctm.d;
          return { x: xn, y: yn };
        }
        var circle = document.getElementById(t),
        cx = +circle.getAttribute('cx'),
        cy = +circle.getAttribute('cy'),
        ctm = circle.getCTM(),
        coords = getScreenCoords(cx, cy, ctm);
         console.log(coords.x, coords.y);

      window.div.transition()    
            .duration(200)    
            .style("opacity", .9);    
        div .html( "<input id="+"trash-button"+" type="+"image"+" title="+"End Event"+" src="+"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTJW-WQ71uV3QRXF4bEYq8v6JvI7sshASrKLisTWLTWVLF3jqkj"+" alt="+"trash"+" style="+"width:25px;"+" >"+"<br>"+"<button onclick="+"trashfuntion()"+">"+"Click Me"+"</button> ")  
           .style("left", (coords.x+25)+ "px")   
        .style("top", (coords.y+28)+ "px");  

     })
  .on("mouseout", function(){d3.select(this).style("fill", "white");})
  .call(drag);

这个按钮不调用这个函数。你能告诉我这个代码有什么错误吗

问题是您已将
onclick
侦听器设置为图像元素,而不是按钮。代码运行良好

var div=d3.选择(“div”);
div.html(
“+”
“+”+“+”单击我“+”) .style(“左”、“10px”) .样式(“顶部”、“10px”); 函数垃圾函数(){ 警报(“被呼叫”); }
div{
高度:250px;
宽度:250px;
}

问题是您已将
onclick
侦听器设置为图像元素,而不是按钮。代码运行良好

var div=d3.选择(“div”);
div.html(
“+”
“+”+“+”单击我“+”) .style(“左”、“10px”) .样式(“顶部”、“10px”); 函数垃圾函数(){ 警报(“被呼叫”); }
div{
高度:250px;
宽度:250px;
}

与其将
onclick=…
函数合并到您正在使用
html
设置的html字符串中,不如使用
d3.on
注册单击/事件处理程序

示例

d3.选择('div')

.html(“与其将
onclick=…
函数合并到您正在使用
html
设置的html字符串中,不如使用
d3.on
注册单击/事件处理程序

示例

d3.选择('div')

.html('这个答案是正确的,但我无法使用.style(“左”、“10px”).style(“顶”、“10px”)在svg中定位div);您尚未共享用于在代码中创建弹出div的代码。请共享该代码,以便我可以提供帮助。我编辑代码。当鼠标悬停到圆圈上时,我创建此弹出div此答案正确,但我无法使用.style(“左”、“10px”).style(“顶”、“10px”)在svg中定位div;您尚未在代码中共享创建弹出div的代码。请共享该代码,以便我提供帮助。我编辑代码。我在鼠标悬停到圆圈上时创建此弹出div
function trashfuntion(){
     console.log("trash");
 }