简体   繁体   English

SVG'tspan'元素和内容不匹配

[英]SVG 'tspan' element and content not matching

I'm having trouble getting a .on("click") event to work, probably because the displayed text of an element I'm trying to click seems to mismatch its actual DOM element. 我无法使.on("click")事件起作用,可能是因为我尝试单击的元素的显示文本似乎与实际的DOM元素不匹配。

The picture illustrates the issue: when my mouse is on the .sublabel2 element (in the example, simply the string [] ), using Chrome's dev tool, the DOM element is highlighted in another position (blue rectangle upper left). 图片说明了这个问题:当我的鼠标位于.sublabel2元素(在示例中,仅是字符串[] )上时,使用Chrome的dev工具,该DOM元素在另一个位置突出显示(左上方的蓝色矩形)。 Any ideas what could be causing this? 任何想法可能是什么原因造成的?

在此处输入图片说明

The code below is what I am using. 下面的代码是我正在使用的代码。 Here is how I create the main svg:text node: 这是我创建主svg:text节点的方法:

pc.createAxes = function() {
  if (g) pc.removeAxes();

  // Add a group element for each dimension.
  g = pc.svg.selectAll(".dimension")
      .data(pc.getOrderedDimensionKeys(), function(d) {
        return d;
      })
    .enter().append("svg:g")
      .attr("class", "dimension")
      .attr("transform", function(d) {
        return "translate(" + xscale(d) + ")";
      });

 // Add an axis and title.
  myLabelNode = g.append("svg:g")
      .attr("class", "axis")
      .attr("transform", "translate(0,0)")
      .each(function(d) {
        var axisElement = d3.select(this).call( pc.applyAxisConfig(axis, __.dimensions[d]) );

        axisElement.selectAll("path")
            .style("fill", "none")
            .style("stroke", "#222")
            .style("shape-rendering", "crispEdges");

        axisElement.selectAll("line")
            .style("fill", "none")
            .style("stroke", "#222")
            .style("shape-rendering", "crispEdges");
      })
    .append('svg:text') //create a node to append tspans

    setLabels(myLabelNode); 
//I skip more less relevant code here for brevity
}

And how I position the labels/sublabels in a separate setLabels() function: 以及如何将标签/子标签放置在单独的setLabels()函数中:

function setLabels(svg_text_node)  {
 svg_text_node.append('tspan') 
          .attr({
                "text-anchor": "middle",
                "y": -40,
                "x": 0,
                "dy": 0,
                "class": "label"
          })

        .on("dblclick", flipAxisAndUpdatePCP)
        .on("wheel", rotateLabels)

    svg_text_node.append('tspan') 
        .attr({
            "text-anchor": "middle",
            "x": 0,
            "dy": 17,
            "class": "sublabel1"
      })


    svg_text_node.append('tspan') 
        .attr({
            "text-anchor": "middle",
            "x": 0,
            "dy": 14,
            "class": "sublabel2"
      })
 }

It is showing the highlight in the location that the object would be in had you not repositioned it using your either absolute or relative positioning. 如果您未使用绝对或相对位置重新放置对象,它将在该对象所处的位置显示高亮显示。 The reason the click handler is not working is likely a z-index issue. 点击处理程序不起作用的原因很可能是z-index问题。 Try giving the div a z-index higher than the rest of the items on the page and see if your click handler starts working properly. 尝试使div的z-index高于页面上其余项目的水平,然后查看点击处理程序是否开始正常工作。

Whenever you reposition something using absolute or relative positioning, you always need to consider how this will impact z-index and adjust for it accordingly. 每当您使用绝对或相对定位重新放置某物时,始终需要考虑这将如何影响z-index并相应地对其进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM