【问题标题】:D3.js Right click opens popup but does not affect the source svg when pop up closedD3.js 右键单击​​打开弹出窗口,但在弹出窗口关闭时不影响源 svg
【发布时间】:2013-12-27 09:43:46
【问题描述】:

这是我的 d3.js 代码,从这里开始 right click 我正在调用函数 contextmenu

// Display the tiles for each bucket.
var tile = svg.selectAll(".tile")
    .data(buckets)
    .enter().append("g");

// add rect and rect attributes         
tile.append("rect")
//.enter().append("rect")
.attr("class", "tile")
.attr("x", function (d) {
return x(d.country) + margin.left + margin.left_padding;
})
.attr("y", function (d) {
return y(d.distrinct_port_nme);
})
.attr("width", x.rangeBand())
.attr("height", y.rangeBand())
.attr("stroke", "goldenrod")
.attr("fill", function (d) {
return z(d.sum_teu);
})
.on("contextmenu",contextmenu);

这是我的contextmenu 函数:

function contextmenu(){
 var node= d3.select(this);
 var position = d3.mouse(this);
d3.select('#my_custom_menu')
  .style('position', 'absolute')
  .style('left', position[0] + "px")
  .style('top', position[1] + "px")
  .style('display', 'block');

d3.event.preventDefault();
document.getElementById('nodeId').value= node
}   

打开一个带有 id= my_custom_menu 的弹出窗口,这里是 popup 设计:

<div id="my_custom_menu" style="display:none;">
<ul> 
<input type="hidden" name="nodeId" id="nodeId" value="" />
 <li onclick="closepop()" style="cursor:pointer;">Close</li>
 <li style="cursor:pointer;">Change color</li>
</ul>
</div

单击close 会关闭(隐藏)popup:

function closepop(){
d3.select('#my_custom_menu')
 .style('display', 'none');
var selectNode =  document.getElementById('nodeId').value;
selectNode.style("fill", "green");
}

现在在close 上,我尝试设置我右键单击fill green 的矩形。我尝试将right clicked rectangle 的instance 存储起来,同时关闭时尝试将fill 和green 放在矩形上。但不工作。任何人都可以帮助我解决我的错误。提前致谢

【问题讨论】:

  • 在 colseup 函数中,您在 selectNode 中获得了哪个值。?是否尊重矩形节点或空..?
  • 我正在获取该矩形节点的对象,如果我这样做 alert selectNode 它显示 [object SVGRectElement]
  • 检查console.log,如果你得到了rect DOM然后给.style.fill="green";检查我的帖子。
  • 查看您的代码在 jsfiddle 中运行良好,因为您正在通过 getElementById 获取圆形对象并将 style.fill 对应于该 circle object。但是在我的代码中,我将 rect 的object 存储在另一个input type hidden field 中,然后在该对象上应用style.fill,从input type 获取值,所以它不起作用。无论style.fill 我在做什么,它都适用于input type hidden field
  • jsfiddle.net/manojmcet/84Utx/4 ...hidden 字段我们只得到字符串而不是相应的对象。在控制台中检查

标签: javascript jquery svg d3.js


【解决方案1】:

在特写功能selectNode 中仅获取string。无法调用样式方法,

试试这个代码:

Fiddle:

var selectNode =  document.getElementById('cir');
     selectNode.style.fill="red";

为了解决您的问题,请选择节点作为全局变量,然后以相同的代码访问。

Fiddle

【讨论】:

    【解决方案2】:

    首先我通过以下方式设置每个元素对应的id:

    .attr("id", function(d) { return "id_" + parseInt(d.x)+"_"+parseInt(d.y)+"_"+Math.floor((Math.random()*100)+1) })
    

    然后在 function contextmenu 我得到 id :

    function contextmenu() {
    var position = d3.mouse(this);
    var node= d3.select(this).attr("id");   
    d3.select('#my_custom_menu')
      .style('position', 'absolute')
      .style('left', position[0]+200 + "px")
      .style('top', position[1] + "px")
      .style('display', 'block');
    
    d3.event.preventDefault();
    document.getElementById('nodeId').value= node
    

    }

    然后在functionclosepop

    function closepop(){
     d3.select('#my_custom_menu')
     .style('display', 'none');
     var selectN=document.getElementById('nodeId').value;
    d3.select('#'+selectN)
    .style('fill', 'green');
    

    }

    【讨论】:

      【解决方案3】:

      试试这个吧……

      var selectNode =  document.getElementById('nodeId');
      selectNode.style.setProperty("fill", "green", "");
      

      【讨论】:

      • 我试过但收到此错误TypeError: selectNode.style is undefined selectNode.style.setProperty("fill", "green", "");
      • 并使用 .attr 而不是 .style?
      • 同样的错误:TypeError: selectNode.attr is undefined selectNode.attr.setProperty("fill", "green", "");
      • 你想变绿是什么? nodeId 是输入元素。如果是 rect,则给 rect 一个 id,然后将 getElementById 更改为引用该 id。
      猜你喜欢
      • 1970-01-01
      • 2012-12-21
      • 2023-02-09
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多