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


HTML Image name用法及代碼示例


HTML DOM中的圖像名稱屬性用於設置或返回<image>元素的名稱屬性的值。

用法:

imageObject.name

屬性值:


  • name:它指定圖像的名稱。

返回值:它返回一個表示圖像名稱的字符串值。

示例1:本示例返回圖像名稱屬性。

<html> 
  
<body> 
    <center> 
  
        <img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190602171808/629.png" 
             id="image"
             alt="" 
             width="250" 
             height="205" 
             name="geeks" 
             class="alignnone size-medium wp-image-1092360" /> 
        <br> 
        <br> 
        <button type="button" onclick="getHeight()"> 
          Click 
      </button> 
        <h4 id="text"></h4> 
    </center> 
    <script> 
        function getHeight() { 
            var imgObject =  
                document.getElementById("image"); 
            var output =  
                document.getElementById("text"); 
            output.innerHTML = "The name of the image is:" 
            + imgObject.name; 
        } 
    </script> 
</body> 
  
</html>

在單擊按鈕之前:

單擊按鈕後:

示例2:本示例設置圖像名稱屬性。

<html> 
  
<body> 
    <center> 
  
        <img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/20190602171808/629.png"
             id="image" 
             alt="" 
             width="250" 
             height="205"
             name="geeks" 
             class="alignnone size-medium wp-image-1092360" /> 
        <br> 
        <br> 
        <button type="button" onclick="getHeight()"> 
          Click 
      </button> 
        <h4 id="text"></h4> 
    </center> 
    <script> 
        function getHeight() { 
            var imgObject =  
                document.getElementById( 
                  "image").name = "Hello Geeks"; 
            
            var output =  
                document.getElementById( 
                  "text").innerHTML =  
                "The name of the image was changed to "  
            + imgObject; 
        } 
    </script> 
</body> 
  
</html>

在單擊按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM Image name屬性支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • 蘋果Safari
  • Opera


相關用法


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