Html merge方法,d3.js selection.merge()用法及代码示例

d3.js中的selection.merge()函数用于将两个给定的选择合并为一个,并在合并后返回新选择。返回的选择具有与此选择相同的组数和相同的父代。

用法:

selection.merge(other);

参数:该函数接受单个参数,其他描述选择将被合并。

返回值:该函数返回一个选择。

范例1:

HTML

1. This text is in bold

2. This text is also in bold

3. Geeks

4. Geeks

5. Geeks for geeks

// Filtering odd children

const odd = d3.selectAll("h3")

.select(function (d, i) {

return i & 1 ? this:null;

});

// Filtering even children

const even = d3.selectAll("h3")

.select(function (d, i) {

return i & 1 ? null:this;

});

// Merging both selections

const merged = odd.merge(even).nodes();

// Printing text content

console.log(

"Collection after merging odd and even is:")

merged.forEach((e) => {

console.log(e.textContent);

});

输出:

65f48a855eed6310e9fe2b84c2a11e1d.png

范例2:

HTML

1. This is odd
2. This is even
3. This is odd
4. This is even
5. This is odd

// Filtering odd children

var even = d3.selectAll("h5")

.select(function (d, i) {

return i & 1 ? this:null

});

// Filtering even children

var odd = d3.selectAll("h5")

.select(function (d, i) {

return i & 1 ? null:this

});

// Merging both selections

const merged = odd.merge(even).nodes();

even = even.nodes();

odd = odd.nodes();

console.log("Odd selection:")

odd.forEach((e) => {

console.log(e.textContent);

});

console.log("Even selection:")

even.forEach((e) => {

console.log(e.textContent);

});

// Printing text content

console.log(

"Collection after merging odd and even is:")

merged.forEach((e) => {

console.log(e.textContent);

});

输出:

5cfce2ac6c016ee19f88891d7db4a482.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值