您可以使用selection.empty()來確定選擇是否爲空。
const isEmpty = d3.selectAll("div").empty();
對於你特別的例子,你可能想要更像這樣的東西。你正在做的是檢查選擇是否爲空,然後使用selection.node()從選擇內容中抓取實際HTMLElement
。
var myselect = d3.select('#myselect select');
if (!myselect.empty()) {
var node = myselect.node();
var options = node.options;
if (options.length > 0) {
}
}
的datum()
功能用於不同的,這將返回的數據對象,所述選擇已經綁定。例如,如果你做的事:
d3.selectAll("div")
.data(["A", "B", "C"])
.enter()
.append("div")
.attr("class", function(d) { return d; }); // will return a letter
// Go select a div and grab the data object it's bound to
// which will return the array item "B", but this could have
// equally been a complex object
d3.select("div.B").datum();
來源
2016-12-14 08:55:15
Ian
嘗試F12和執行console.log(myselect) – mplungjan