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


HTML DOM Map用法及代碼示例


HTML DOM中的Map Object用於創建和訪問<map>元素。Map對象會創建可點擊和可訪問的圖像部分。

用法:

  • 它用於返回Map對象。
    var x = document.getElementById("myMap");
  • 它用於創建Map對象。
    var x = document.createElement("MAP");

屬性值:

  • name:它用於設置或返回image-map的名稱屬性的值。

集合值:

  • 領域:它用於返回image-map中所有<area>元素的集合。
  • 圖片:它用於返回與image-map關聯的所有<img>和<object>元素的集合。

範例1:本示例說明了用於訪問<map>元素的getElementById()方法。



<!DOCTYPE html> 
<html> 
  
<head> 
    <title>  
        HTML DOM Map Object Property  
    </title> 
</head> 
  
<body> 
      
    <h4>Click the button</h4> 
      
    <button onclick = "GFG()"> 
        Click Here! 
    </button> 
      
    <p></p> 
      
    <map id = "Geeks" name = "Geeks"> 
          
        <area  = "rect" coords = "0, 0, 110, 100"
        alt = "Geeks" href =  
"https://media.geeksforgeeks.org/wp-content/uploads/a1-21.png"> 
  
        <area shape = "rect" coords = "110, 0, 190, 100"
        alt = "For" href = 
"https://media.geeksforgeeks.org/wp-content/uploads/a1-22.png"> 
  
        <area shape = "rect" coords = "190, 0, 300, 100"
        alt = "GEEKS" href = 
"https://media.geeksforgeeks.org/wp-content/uploads/a1-24.png"> 
    </map> 
      
    <img src = 
"https://media.geeksforgeeks.org/wp-content/uploads/a1-25.png"
    width = "300" height = "100" alt="GFG" usemap = "#Geeks"> 
      
    <p id = "GEEK!"></p> 
      
    <script> 
        function GFG() { 
            var x = document.getElementById("Geeks").areas.length; 
            document.getElementById("GEEK!").innerHTML = x; 
        } 
    </script> 
</body
  
</html>                    

輸出:

範例2:本示例介紹了用於創建<map>元素的document.createElement()方法。

<!DOCTYPE html> 
<html> 
      
<head> 
    <title>  
        HTML DOM Map Object Property  
    </title> 
</head> 
      
<body> 
    <h4>Click the button</h4> 
      
    <button onclick = "GFG()"> 
        Click Here! 
    </button> 
      
    <p></p> 
      
    <img src =  
"https://media.geeksforgeeks.org/wp-content/uploads/a1-25.png"
    width = "300" height = "100" usemap = "#myMap"> 
      
    <p id = "GEEK!"></p> 
      
    <!-- script to use map object property -->
    <script> 
        function GFG() { 
            var x = document.createElement("MAP"); 
            x.setAttribute("id", "myMap"); 
            x.setAttribute("name", "myMap"); 
            document.body.appendChild(x); 
  
            var y = document.createElement("AREA"); 
            y.setAttribute("href",  
"https://media.geeksforgeeks.org/wp-content/uploads/a1-24.png"); 
            y.setAttribute("shape", "rect"); 
            y.setAttribute("coords", "190, 0, 300, 100"); 
            document.getElementById("myMap").appendChild(y); 
  
            document.getElementById("GEEK!").innerHTML =  
            "Click on the GEEKS area in the image."; 
        }      
    </script> 
</body> 
  
</html>                    

輸出:

支持的瀏覽器:下麵列出了DOM Map Object屬性支持的瀏覽器:

  • 穀歌瀏覽器5.0
  • Internet Explorer 8.0
  • Firefox 3.6
  • Safari 5.0
  • Opera 10.6




相關用法


注:本文由純淨天空篩選整理自SHUBHAMSINGH10大神的英文原創作品 HTML | DOM Map Object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。