d3.select仅包含元素

时间:2018-05-16 13:17:10

标签: javascript d3.js

如何将d3.selectAll与元素中的类一起使用。

这是一个描述我的问题的例子:

请不要jquery,没有纯粹的javascript。我需要d3.selectAll因为我有d3数据绑定

JS FIDDLE

HTML

<div id="one">
<p class="test"></p>
<p class="test"></p>
</div>

<div id= "two">
<p class="test"></p>
<p class="test"></p>
</div>

JS

let one = document.getElementById("one")
let two = document.getElementById("two")
let allElements = d3.selectAll(".test")
console.log(allElements)
// Now i want only from div one
let div1Elements = d3.selectAll("???")
// Now i want only from div two
let div2Elements = d3.selectAll("???")

1 个答案:

答案 0 :(得分:3)

您可以使用

let div1Elements = d3.selectAll("#one .test");
// Now i want only from div two
let div2Elements = d3.selectAll("#two .test");

或者像这样

&#13;
&#13;
let one = d3.select("#one")
// Now i want only from div one
let div1Elements = one.selectAll(".test");
console.log(div1Elements);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="one">
<p class="test">a</p>
<p class="test">b</p>
</div>

<div id= "two">
<p class="test"></p>
<p class="test"></p>
</div>
&#13;
&#13;
&#13;