JQuery 和 SVG - 如何找到“g”标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17043318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:18:41  来源:igfitidea点击:

JQuery and SVG - how to find the 'g' tag

jquerysvgjquery-svg

提问by Kungen

Imagine this:

想象一下:

<svg>
    <g id="node1" class="node"></g>
    <g id="node2" class="node"></g>
</svg>

How can I find the 'g' tag, I want that all tags could be clicked, and not just 'node1' or 'node2'. I've tried simular to this, but could not get it work.

我怎样才能找到“g”标签,我希望所有标签都可以被点击,而不仅仅是“node1”或“node2”。我试过与此类似,但无法正常工作。

$('g').click(function(){
    alert("Hellooooo");
});

回答by Bhojendra Rauniyar

Use find() method for more info visit this

使用 find() 方法获取更多信息,请访问这里

For eg $("body").find("p").css("background-color","#f00");sets all body's <p>element background-color to red.

例如,$("body").find("p").css("background-color","#f00");将所有主体的<p>元素背景颜色设置为红色。

For your question try this:

对于您的问题,请尝试以下操作:

$("svg").find("g").click(function(){

// your jquery code here

}
);

回答by Kungen

Thanks to C-Link I've solved this.

感谢 C-Link 我解决了这个问题。

To be sure only the nodes are clickable I've wroted:

为了确保只有节点是可点击的,我写道:

$("svg").find("g.node").click(function(){
    alert("Lolol");
});

And it works fine.

它工作正常。