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


HTML DOM Image用法及代碼示例


HTML DOM中的Image對象用於表示HTML <image>元素。此標記用於設置或獲取<image>元素的屬性。可以使用getElementById()方法訪問此元素。

用法:

document.getElementById("Image_ID");

該Image_ID分配給HTML <image>元素。

示例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <base id="Geek_Base"
          href="https://www.geeksforgeeks.org"> 
    
    <title> 
        HTML | DOM Image Object 
    </title> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>HTML | DOM Image Object</h2> 
  
    <button onclick="myGeeks()"> 
        Click 
    </button> 
    <h4><p id="Geek_p" 
           style="color:green"> 
      </p></h4> 
  
    <script> 
        function myGeeks() { 
  
            //   Creating IMG object. 
            var x = document.createElement("IMG"); 
            
            x.setAttribute("src",  
"https://media.geeksforgeeks.org/wp-content/uploads/gfg-39.png"); 
            
            x.setAttribute("width", "304"); 
            x.setAttribute("height", "228"); 
            x.setAttribute("alt", "GFG_Logo"); 
            document.body.appendChild(x); 
        } 
    </script> 
</body> 
  
</html>

輸出



  • 在單擊按鈕之前:
  • 單擊按鈕後:
  • >

    示例-2:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <base id="Geek_Base"
              href="https://www.geeksforgeeks.org"> 
        <title> 
            HTML | DOM Image Object 
        </title> 
    </head> 
      
    <body style="text-align:center;"> 
      
        <h1 style="color:green;">   
                GeeksForGeeks   
            </h1> 
      
        <h2>HTML | DOM Image Object</h2> 
        <img id="myImg"
    src="https://media.geeksforgeeks.org/wp-content/uploads/gfg-39.png" 
             alt="GFG_Logo"> 
        <br> 
        
        <button align="center" onclick="myGeeks()"> 
            Click 
        </button> 
        <h4><p id="Geek_p" style="color:green"></p></h4> 
      
        <script> 
            function myGeeks() { 
      
                //  Accessing image element. 
                var x =  
                document.getElementById("myImg").src; 
                
                document.getElementById( 
                  "Geek_p").innerHTML = x; 
            } 
        </script> 
    </body> 
      
    </html>

    輸出

    • 在單擊按鈕之前:
    • 單擊按鈕後:

    示例3:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <base id="Geek_Base" 
              href="https://www.geeksforgeeks.org"> 
        <title> 
            HTML | DOM Image Object 
        </title> 
    </head> 
      
    <body style="text-align:center;"> 
      
        <h1 style="color:green;">   
                GeeksForGeeks   
            </h1> 
      
        <h2>HTML | DOM Image Object</h2> 
        <img id="myImg" 
    src="https://media.geeksforgeeks.org/wp-content/uploads/gfg-39.png"
             alt="GFG_Logo"> 
        <br> 
        <button align="center" onclick="myGeeks()"> 
            Click 
        </button> 
        <h4><p id="Geek_p" style="color:green"></p></h4> 
      
        <script> 
            function myGeeks() { 
      
                //  Return Image width. 
                var x = document.getElementById( 
                  "myImg").width; 
                
                document.getElementById( 
                  "Geek_p").innerHTML = x; 
            } 
        </script> 
    </body> 
      
    </html>

    輸出

    • 在單擊按鈕之前:
    • 單擊按鈕後:

    支持的瀏覽器:

    • 穀歌瀏覽器
    • 火狐瀏覽器
    • Edge
    • Safari
    • Opera

相關用法


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