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


d3.js selection.remove()用法及代碼示例

selection.remove()函數用於從文檔中刪除所選元素,並使用刪除的元素返回新選擇。此外,新的選擇現在已從DOM分離

用法:

selection.remove();

參數:該函數不接受任何參數。

返回值:該函數返回一個新的選擇。

範例1:



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> 
        h1 { 
            color:green; 
        } 
  
        div { 
            width:300px; 
            color:#ffffff; 
            height:50px; 
            background-color:green; 
            margin:10px; 
        } 
    </style> 
  
<body> 
    <h1>GeeksforGeeks</h1> 
  
    <h4>D3.js selection.remove() Function</h4> 
  
    <p>The divs will be removed.</p> 
  
    <div><span>1. This div will be removed.</span></div> 
    <div><span>2. This div will be removed.</span></div> 
  
    <button>Click Here!</button> 
  
    <script> 
        function func() { 
            // Selecting  div and 
            // Removing the div 
            var div = d3.selectAll("div") 
                .remove(); 
        } 
        btn = document.querySelector("button"); 
        btn.addEventListener("click", func); 
  
    </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> 
  
    <style> 
        h1 { 
            color:green; 
        } 
  
        div { 
            width:300px; 
            color:#ffffff; 
            height:50px; 
            background-color:green; 
            margin:10px; 
        } 
    </style> 
  
<body> 
    <h1>Geeks for geeks</h1> 
  
    <h4>D3.js selection.remove() Function</h4> 
  
    <p>The <span> from div will be removed.</p> 
  
    <div><span>1. This text will be removed.</span></div> 
    <div><span>2. This div will not be removed.</span></div> 
  
    <button>Click Here!</button> 
  
    <script> 
        function func() { 
            // Selecting  div and 
            // The text inside the div will be removed. 
            var div = d3.select("span") 
                .remove(); 
        } 
        btn = document.querySelector("button"); 
        btn.addEventListener("click", func); 
    </script> 
</body> 
  
</html>

輸出:

在單擊“單擊此處”按鈕之前:

單擊“單擊此處”按鈕後:




相關用法


注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js selection.remove() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。