使用选择器检索填充和描边颜色?
Using selectors to retrieve fill and stroke colors?
对 d3 和选择器的概念非常陌生 - 只是想知道是否可以使用选择器检索元素的颜色?
例如:
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//Draw the line
var circle = svgContainer.append("line")
.attr("x1", 5)
.attr("y1", 5)
.attr("x2", 50)
.attr("y2", 50)
.attr("stroke-width", 2)
.attr("stroke", "black");
我需要做什么才能使用选择器检索 "stroke" 属性?是否可以做类似 circle.select("stroke") 的事情并让它给我值 "black" 或类似的东西......?
提前致谢!
您混淆了元素及其属性 -- select或 select 前者,但不是后者。然而,一旦您 select 编辑了一个元素,您就可以查询它的属性:
var black = d3.select("line").attr("stroke");
或直接使用D3 select离子:
var black = circle.attr("stroke");
对 d3 和选择器的概念非常陌生 - 只是想知道是否可以使用选择器检索元素的颜色?
例如:
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//Draw the line
var circle = svgContainer.append("line")
.attr("x1", 5)
.attr("y1", 5)
.attr("x2", 50)
.attr("y2", 50)
.attr("stroke-width", 2)
.attr("stroke", "black");
我需要做什么才能使用选择器检索 "stroke" 属性?是否可以做类似 circle.select("stroke") 的事情并让它给我值 "black" 或类似的东西......?
提前致谢!
您混淆了元素及其属性 -- select或 select 前者,但不是后者。然而,一旦您 select 编辑了一个元素,您就可以查询它的属性:
var black = d3.select("line").attr("stroke");
或直接使用D3 select离子:
var black = circle.attr("stroke");