selection.node()
返回only the first節點。我們可以從選擇中獲得所有節點的數組嗎?從d3選擇獲取所有dom節點
編輯添加了一些代碼來幫助我們。
- 與
each()
的嘗試產生希望 輸出只有一個,雖然非常詳細。 - 調用
sel[0]
也返回一個帶有DOM節點的數組,但它很不方便(取決於庫的內部結構),並且包含一個不需要的「parentNode」字段。
// creating a selection to experiment with
var data= [1,2,3,4]
var sel = d3.select("li")
\t .data(data)
\t .enter().append("li").html(identity);
function identity(d){return d}
console.log(sel); // array[1] with array[4] with the <li>'s
// using .node()
var res1 = sel.node();
console.log(res1); // first <li> only
// using .each() to accumulate nodes in an array
var res2 = [];
function appendToRes2(){
\t res2.push(this);
}
sel.each(appendToRes2);
console.log(res2); // array[4] with the <li>'s (what I want)
// calling sel[0]
var res3 = sel[0];
console.log(res3); // array[4] with the <li>'s plus a "parentNode"
// @thisOneGuy's suggestion
var res4 = d3.selectAll(sel);
console.log(res4); // array[1] with array[1] with array[4] with the <li>'s
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
EDIT 2爲什麼我要這麼做?
在DOM節點上調用array methods,如reduce
和map
。 D3提供了filter
,但要使用其他人我首先需要從選擇中提取節點數組。
怎麼樣selection.each(功能)。它在你提供的鏈接 – Mike
是的,我可以調用'each',從'this'獲取dom節點並將它們累加到一個外部變量中。看起來'selection [0]'也是一樣的。我想知道是否沒有更清潔/更簡單的方法。 – Vituel
d3.selectAll(選擇)?添加一些代碼,以便我可以更好地理解您的問題 – thatOneGuy