[英]Select content of text element with tspan specifier in SVG with D3js
[英]SVG 'tspan' element and content not matching
我无法使.on("click")
事件起作用,可能是因为我尝试单击的元素的显示文本似乎与实际的DOM元素不匹配。
图片说明了这个问题:当我的鼠标位于.sublabel2
元素(在示例中,仅是字符串[]
)上时,使用Chrome的dev工具,该DOM元素在另一个位置突出显示(左上方的蓝色矩形)。 任何想法可能是什么原因造成的?
下面的代码是我正在使用的代码。 这是我创建主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
}
以及如何将标签/子标签放置在单独的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"
})
}
如果您未使用绝对或相对位置重新放置对象,它将在该对象所处的位置显示高亮显示。 点击处理程序不起作用的原因很可能是z-index问题。 尝试使div的z-index高于页面上其余项目的水平,然后查看点击处理程序是否开始正常工作。
每当您使用绝对或相对定位重新放置某物时,始终需要考虑这将如何影响z-index并相应地对其进行调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.