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


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


selection.html()函数用于在所有选定元素上设置内部HTML。如果该值是常数,则所有元素都被赋予相同的值。空值将清除元素的内容。

用法:

selection.html([value]);

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

  • Value:它是将HTML设置为文档的字符串类型。

返回值:此函数不返回任何内容。

范例1:在以下代码中,HTML “p”元素内容更改为“bold”文本。



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> 
  
    <style> 
        p { 
            background-color:#f2f2f2; 
            padding:10px; 
            width:200px; 
            line-height:5px; 
        } 
  
        p:hover { 
            background-color:grey; 
            padding:10px; 
            cursor:pointer; 
        } 
    </style> 
</head> 
  
<body> 
    <div> 
        <h1 style="color:green;"> 
            GeeksforGeeks 
        </h1> 
          
        <h4>D3.js selection.html() Function</h4> 
  
        <p>Click Here!</p> 
    </div> 
  
    <script> 
        function func() { 
  
            // Selecting all p and setting 
            // the innerHTML of the p 
            var chk = d3.selectAll("p") 
                .html("<b>This is from .html</b>"); 
            var text = document.querySelector("p"); 
        } 
        let btn = document.querySelector("p"); 
        btn.addEventListener("click", func); 
    </script> 
</body> 
  
</html>

输出:

范例2:下面的示例使用null清除内容。

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> 
    <style> 
        p { 
            background-color:#f2f2f2; 
            padding:10px; 
            width:200px; 
            line-height:5px; 
        } 
  
        p:hover { 
            background-color:grey; 
            padding:10px; 
            cursor:pointer; 
        } 
    </style> 
</head> 
  
<body> 
    <div> 
        <h1 style="color:green;"> 
            GeeksforGeeks 
        </h1> 
  
        <h4>D3.js selection.html() Function</h4> 
  
        <p><b>Click Here</b></p> 
    </div> 
  
    <script> 
        function func() { 
  
            // Selecting p and setting the 
            // innerHTML of the p 
            var chk = d3.selectAll("p") 
                .html(null); 
            var text = document.querySelector("p"); 
        } 
        let btn = document.querySelector("p"); 
        btn.addEventListener("click", func); 
    </script> 
</body> 
  
</html>

输出:




相关用法


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