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


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


d3.creator()函数用于返回一个函数,该函数创建一个元素,该元素的名称作为函数中的参数给出。

用法:

d3.creator( name );

参数:该函数接受上述和以下描述的单个参数:

  • name:它是要创建的容器或HTML标记的名称。

返回值:它返回一个函数。

范例1:在此示例中,在主体内添加div元素。



HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" path1tent= 
        "width=device-width,initial-scale=1.0"> 
    <script src= 
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
    <script src= 
"https://d3js.org/d3-selection.v1.min.js"> 
    </script> 
  
    <style> 
        div { 
            background-color:green; 
            color:honeydew; 
            width:fit-content; 
            padding:10px; 
        } 
    </style> 
</head> 
  
<body> 
    <!-- No div tag is added here -->
  
    <script> 
        let selection = d3.select("body") 
  
        // Creating and appending 
        // a div to the body 
        selection.append(d3.creator("div")); 
        let div = document.querySelector("div") 
          
        div.innerText =  
            "Div tag created using d3.creator()" 
    </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"> 
    <script src= 
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
    <script src= 
"https://d3js.org/d3-selection.v1.min.js"> 
    </script> 
  
    <style> 
        div { 
            background-color:green; 
            color:honeydew; 
            width:fit-content; 
            padding:10px; 
            height:100px; 
        } 
    </style> 
</head> 
  
<body> 
    <!-- No div tag is added here -->
  
    <script> 
        var selection = d3.select("body") 
  
        // Creating and appending a  
        // div to the body 
        selection.append(d3.creator("div")); 
        var selection = d3.select("body") 
  
        // Creating and appending a p 
        // tag to the div 
        selection.append(d3.creator("p")); 
  
        // Creating and appending a button 
        // tag to the div 
        selection.append(d3.creator("button")); 
        var div = document.querySelector("div") 
  
        div.innerHTML =  
            "Div tag created using d3.creator()" 
  
        var div = document.querySelector("p") 
  
        div.innerHTML =  
            "paragraph tag created using d3.creator()" 
              
        var div = document.querySelector("button") 
  
        div.innerHTML =  
            "paragraph tag created using d3.creator()" 
    </script> 
</body> 
  
</html>

输出:




相关用法


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