[英]Get the closest element to a SVG node using Jquery closest() function
I have the following code: 我有以下代码:
$(document).ready(function () {
var svg = d3.select("svg");
svg.append("circle")
.attr("class","cursor")
.attr("r", 8)
.attr("cx", 100)
.attr("cy", 100)
.on("click", test);
function test(d)
{
f = d3.select(this);
console.log($(f).closest('svg').css('border-color','red'));
}
});
I noticed that it doesn't work. 我注意到它不起作用。 But if I execute the same function on a non SVG element (regular html element like div or canvas...) it does what it is supposed to do.
但是,如果我在非SVG元素(常规html元素,例如div或canvas ...)上执行相同的功能,它将执行应做的事情。 I first thought that the problem was the d3 selection.
我首先认为问题是d3选择。 I then parsed the element directly with the class attribute like:
然后,我直接使用class属性解析元素,例如:
console.log($('.cursor').closest('svg').css('border-color','red'));
Without success. 没有成功。
Is there a way to make the jquery closest() function work with element inside SVG tag ? 有没有一种方法可以使jQuery最近的()函数与SVG标签中的元素一起使用?
Thank you 谢谢
Wrapping a d3 selection in $
is invalid. 将d3选择包装在
$
是无效的。 Use $(this)
instead of $(f)
. 使用
$(this)
代替$(f)
。 The this
keyword in the click handler is set to the clicked DOM element, which is what you need. 单击处理程序中的
this
关键字设置为被单击的DOM元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.