[英]button ouside d3.js svg to select and modify d3.js elements
I have svg graph generated via d3.js, and click events attached to DOM generated by d3.js works, but a precreated DOM outside d3.js simply does not fire the event 我通过d3.js生成了svg图,并且d3.js生成的DOM上的单击事件起作用了,但是d3.js之外的预先创建的DOM根本不会触发该事件
//d3.js generated DOM
var marker = layer.selectAll("svg")
.data(data)
.each(transform) // update existing markers
.enter().append("svg")
.each(transform)
.attr("class", function (d) {
return d[12] === "Region" ? "marker region" : "marker";
});
//pre-created DOM
var btn = d3.select('#vslCur_btn0');
//method i'd like to trigger on click
var hideAct = function(){
console.log("trying to hide"+marker.size());
marker.filter(function (d) {
return d[12] === "Region";
}).style("width", function (d) {
console.log('attr lulz:' + d3.select(this).style('width'));
return d3.select(this).style('width') === '70px' ? '0px' : '70px';
});
};
marker.on("click", hideAct);//works when clicked displays "trying to hide295" or the number of svg
btn.on("click", hideAct);//does not work but displays "trying to hide0" on console
Obviously #vslCur_btn0
exists outside the svg. 显然
#vslCur_btn0
存在于svg之外。 In case selectAll("svg")
bothers you there are multiple absolute position svgs within a div which serves as some sort of canvas (google maps), #vslCur_btn0
exists outside of that canvas and is some sort of control panel. 如果
selectAll("svg")
困扰您,则div中有多个绝对位置selectAll("svg")
用作某种画布(谷歌地图), #vslCur_btn0
存在于该画布之外,并且是某种控制面板。 the questions is 问题是
#vslCur_btn0
as outside scope #vslCur_btn0
视为外部范围 click events attached to DOM generated by d3.js work, but a precreated DOM outside d3.js simply does not fire the event.
d3.js生成的DOM附带的click事件起作用,但是d3.js外部的预先创建的DOM根本不会触发该事件。
This is wrong, it does fire. 这是错误的,确实会触发。 The problem here seems to be the use of
this
. 这里的问题似乎是使用
this
。
In your hideAct
function, you are using this
to change the style of a given svg element. 在
hideAct
函数中,您正在使用this
来更改给定svg元素的样式。 The problem is, when you click the button, this
is the button itself, not the SVG element. 问题是,当您单击按钮时,
this
是按钮本身,而不是SVG元素。
Here is a fiddle demo, that calls a function on click to log the radius of the circle: https://jsfiddle.net/gerardofurtado/42yLuhfb/ 这是一个小提琴演示,它在单击时调用一个函数以记录圆的半径: https : //jsfiddle.net/gerardofurtado/42yLuhfb/
I'm using the same pattern in your code: 我在您的代码中使用了相同的模式:
circles.on("click", test);//clicking on the SVG element
d3.select("#btn").on("click", test);//clicking on the HTML button
Click the circle first, and after that click the button, comparing the results. 首先单击圆圈,然后单击按钮,比较结果。 Pay attention to the console, it logs
this
and the r
. 注意控制台,它记录了
this
和r
。 For the button, the r
result is, of course, null
对于按钮,
r
结果当然为null
PS : I'd rather use SO snippet instead of an external link (JSfiddle), but unfortunately SO snippet is freezing when we try to console.log this
. PS :我宁愿使用SO片段代替外部链接(JSfiddle),但是不幸的是,当我们尝试console.log
this
时,SO片段冻结了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.