javascript - D3 .classed() Using multiple selectors (Version 4) - Stack Overflow

Using version 4 of D3 and trying to select the third item, remove the item class, and add the highlight

Using version 4 of D3 and trying to select the third item, remove the item class, and add the highlight and bigger classes. For some reason the item class is not being removed. And suggestions?

HTML Code

  <div class="container">
      <h2>D3 Graphic</h2>
      <section id="chart">
           <div class="item">Barot Bellingham</div>
           <div class="item">Hassum Harrod</div>
           <div class="item">Jennifer Jerome</div>
           <div class="item">Richard Tweet</div>
           <div class="item">Lorenzo Garcia</div>
           <div class="item">Xhou Ta</div>
      </section>
 </div>

D3 Code

d3.selectAll('.item:nth-child(3)')
.classed(
    'highlight', true,
    'item', false,
    'bigger', true
);

Using version 4 of D3 and trying to select the third item, remove the item class, and add the highlight and bigger classes. For some reason the item class is not being removed. And suggestions?

HTML Code

  <div class="container">
      <h2>D3 Graphic</h2>
      <section id="chart">
           <div class="item">Barot Bellingham</div>
           <div class="item">Hassum Harrod</div>
           <div class="item">Jennifer Jerome</div>
           <div class="item">Richard Tweet</div>
           <div class="item">Lorenzo Garcia</div>
           <div class="item">Xhou Ta</div>
      </section>
 </div>

D3 Code

d3.selectAll('.item:nth-child(3)')
.classed(
    'highlight', true,
    'item', false,
    'bigger', true
);
Share Improve this question asked Jul 21, 2016 at 16:46 Dani MossDani Moss 732 gold badges2 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

EDIT : for v4. You can write a function but, it is simpler this way.

d3.selectAll('.item:nth-child(3)')
    .classed('highlight bigger',true)
    .classed('item',false);
 .highlight {
    color : red;
    }
    .item {
    font-weight : bold;
    }
    .bigger {
    font-size : 24px;
    }
<script src="https://d3js/d3.v4.min.js"></script>
<div class="container">
          <h2>D3 Graphic</h2>
          <section id="chart">
               <div class="highlight">Barot Bellingham</div>
               <div class="item">Hassum Harrod</div>
               <div class="item">Jennifer Jerome</div>
               <div class="item">Richard Tweet</div>
               <div class="item">Lorenzo Garcia</div>
               <div class="bigger">Xhou Ta</div>
          </section>
     </div>

Original answer : Your syntax is wrong.

d3.selectAll('.item:nth-child(3)')
.classed({
    'highlight': true,
    'item': false,
    'bigger': true
});
.highlight {
color : red;
}
.item {
font-weight : bold;
}
.bigger {
font-size : 24px;
}
<script src="https://cdnjs.cloudflare./ajax/libs/d3/3.4.10/d3.min.js"></script>
<div class="container">
      <h2>D3 Graphic</h2>
      <section id="chart">
           <div class="highlight">Barot Bellingham</div>
           <div class="item">Hassum Harrod</div>
           <div class="item">Jennifer Jerome</div>
           <div class="item">Richard Tweet</div>
           <div class="item">Lorenzo Garcia</div>
           <div class="bigger">Xhou Ta</div>
      </section>
 </div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744232142a4564304.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信