最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - d3 select everything with specific CSS class - Stack Overflow

programmeradmin0浏览0评论

I am trying to select all elements with a certain class. They might have different classes before, or after this class, but as long as they have this class they should be selected

ex:

<g class="classa classb classc">
<g class="classq classr classz">
<g class="classd classb classe">

I wish to select the 1st and 3rd elements, because they have are both under classb. Any suggestions? I thought d3.selectAll(".classb") would work, but it doesn’t.

I am trying to select all elements with a certain class. They might have different classes before, or after this class, but as long as they have this class they should be selected

ex:

<g class="classa classb classc">
<g class="classq classr classz">
<g class="classd classb classe">

I wish to select the 1st and 3rd elements, because they have are both under classb. Any suggestions? I thought d3.selectAll(".classb") would work, but it doesn’t.

Share Improve this question edited May 25, 2024 at 7:47 Roly 2,2383 gold badges21 silver badges35 bronze badges asked Nov 25, 2014 at 21:07 As3adTintinAs3adTintin 2,47612 gold badges34 silver badges62 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

When you're working with multiple classes in web development, remember that they are separated by a space in the class attribute.

So something like this

<g class="class_a class_b class_c">
<g class="class_z class_r class_z">
<g class="class_d class_b class_e">

Would be how you would get proper access to these elements.

Once you start naming these properly, you should be able to select them via CSS. So, classes are going to be a single word, and are separated by spaces (not commas).

Then try: d3.selectAll('.class_b')

Classes are separated by spaces, not commas!

<g class="classa classb classc">
<g class="classq classr classz">
<g class="classd classb classe">

or better yet,

<g class="class-a class-b class-c">
<g class="class-q class-r class-z">
<g class="class-d class-b class-e">

with the selector being d3.selectAll(".class-b").

Classes and selections for svg elements are the same as for html elements. Your selection of d3.selectAll(".class b") would be selecting all <b> elements who are children of elements with the class class.

发布评论

评论列表(0)

  1. 暂无评论