请注意,目前没有专用的 API 可以将删除的元素添加回文档;但是,您可以将函数传递给selection.append 或selection.insert 来重新添加元素。
https://github.com/mbostock/d3/wiki/Selections
传递函数是什么意思?我不知道如何实施这个。我尝试传递一个函数,将删除的 div 附加回父级,但没有成功。有任何想法吗?
这是 d3 中相对较新的功能。
a function that returns the DOM element to append
所以你可以简单地做类似的事情:
d3.select("body").append(function() {
return document.createElement("div");
});
Run Code Online (Sandbox Code Playgroud)
而不是简单地使用字符串名称
d3.select("body").append("div");
Run Code Online (Sandbox Code Playgroud)
当您使用 d3 选择器时,请记住它们不是单个元素。他们是一系列群体。因此,如果你说:
var divs = d3.select("div").remove(); // actually a multidimensional array
var firstElement = divs[0][0]; // this can be appended by returning it
Run Code Online (Sandbox Code Playgroud)
迈克的相关文字是:One nuance is that selections are grouped: rather than a one-dimensional array, each selection is an array of arrays of elements
这是一个简单的工作小提琴,展示了一个非常基本的示例。
| 归档时间: |
|
| 查看次数: |
2193 次 |
| 最近记录: |