当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


d3.js selection.enter()用法及代码示例


D3.js中的d3.selection.enter()函数用于创建缺少的元素并返回回车选择。

用法:

selection.enter(); 

参数:此函数不接受任何参数。

返回值:此函数返回回车选择。

以下示例说明了JavaScript中的D3.js selection.enter()函数:



范例1:

HTML

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
        <meta charset="UTF-8" /> 
        <meta
            name="viewport"
            path1tent="width=device-width,  
                       initial-scale=1.0"/> 
        <title>D3.js selection.enter() Function</title> 
    </head> 
    <style> 
        div { 
            background-color:green; 
            color:#ffffff; 
            width:50px; 
            margin-bottom:5px; 
            padding:20px; 
            height:10px; 
        } 
    </style> 
    <body> 
        <!-- Please note that no div tags are added here -->
        <script src= 
"https://d3js.org/d3.v4.min.js"> 
        </script> 
        <script src= 
"https://d3js.org/d3-selection.v1.min.js"> 
        </script> 
        <script> 
            var div = d3 
                .select("body") 
                .selectAll("div") 
                .data(["geeks", "for", "geeks"]) 
                .enter() 
                .append("div") 
                .text((d) => { 
                    return d; 
                }); 
        </script> 
    </body> 
</html>

输出:

范例2:

HTML

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
        <meta charset="UTF-8" /> 
        <meta
            name="viewport"
            path1tent="width=device-width,  
                       initial-scale=1.0"/> 
        <title>D3.js selection.enter() Function</title> 
    </head> 
    <style> 
        div { 
            background-color:green; 
            color:#ffffff; 
            width:50px; 
            margin-bottom:5px; 
            padding:20px; 
            height:10px; 
        } 
    </style> 
    <body> 
        <!-- Please note that no div tags are added here -->
        <script src= 
"https://d3js.org/d3.v4.min.js"> 
        </script> 
        <script src= 
"https://d3js.org/d3-selection.v1.min.js"> 
        </script> 
        <script> 
            var h2 = d3 
                .select("body") 
                .selectAll("h2") 
                .data(["I am from heading level 2"]) 
                .enter() 
                .append("h2") 
                .text((d) => { 
                    return d; 
                }); 
            var span = d3 
                .select("body") 
                .selectAll("span") 
                .data("I am from span") 
                .enter() 
                .append("span") 
                .text((d) => { 
                    return d; 
                }); 
            var p = d3 
                .select("body") 
                .selectAll("p") 
                .data(["paragraph", "paragraph"]) 
                .enter() 
                .append("p") 
                .text((d) => { 
                    return d; 
                }); 
        </script> 
    </body> 
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自tarun007大神的英文原创作品 D3.js selection.enter() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。