當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。