[英]d3js - adding a parent element to a selection
How can i add with d3.js a parent element to a selection. 如何使用d3.js将父元素添加到选择中。 Lets say I have the following
可以说我有以下
<div class="i need a parent">...</div>
<div class="i need a parent">...</div>
and i want to modify this into 我想将其修改为
<a href="...">
<div class="i need a parent">...</div>
</a>
<a href="...">
<div class="i need a parent">...</div>
</a>
Any suggestion highly appreciated. 任何建议高度赞赏。
Not possible purely with d3, but easy enough with a little standard JavaScript 单纯使用d3无法实现,但是使用一些标准JavaScript即可轻松实现
var selection = d3.selectAll('div.needparent');
selection.each(function() {
var oldParent = this.parentNode;
var newParent = document.createElement('a');
oldParent.replaceChild(newParent, this);
newParent.appendChild(this);
})
You can use d3 for some of the operations within the iteration, but since replaceChild
requires standard JavaScript I stuck with that throughout. 您可以在迭代中的某些操作中使用d3,但是由于
replaceChild
需要标准JavaScript,因此我始终坚持使用d3。
One note of caution, if you have event handlers bound to the child elements, I'm not sure they'll be preserved with this approach. 请注意,如果您将事件处理程序绑定到子元素,则我不确定使用这种方法会保留它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.