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


HTML Anchor download用法及代碼示例


HTML DOM中的DOM Anchor下載屬性用於設置或返回鏈接的download屬性的值。 download屬性用於指定用戶單擊時將下載的目標鏈接。

用法:

  • 它返回下載屬性。
    anchorObject.download
  • 它用於設置下載屬性。
    anchorObject.download = filename

屬性值:


  • filename:它指定必須下載的文件的名稱。

返回值:它返回一個代表下載文件名稱的字符串值。

示例1:本示例返回下載屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Anchor download Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1>GeeksForGeeks</h1> 
  
        <h2> 
          DOM Anchor download Property 
      </h2> 
  
        <a href= 
    "http://www.example.com:4097/test.htm#part2" 
           id="GFG"
           rel="nofollow" 
           hreflang="en-us"
           target="_self" 
           download="GFG_Logo"> 
            
            <img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/gfg1-11.png" 
                 width="300" 
                 height="250"> 
        </a> 
  
        <BR> 
        <button onclick="myGeeks()"> 
          Submit 
      </button> 
  
        <p id="sudo" 
           style="color:green; 
                  font-size:25px;"> 
      </p> 
  
        <script> 
            function myGeeks() { 
                var x =  
                    document.getElementById( 
                      "GFG").download; 
                
                document.getElementById( 
                  "sudo").innerHTML = x; 
            } 
        </script> " 
    </center> 
</body> 
  
</html>

輸出:

在單擊按鈕之前:

單擊按鈕後:

示例2:本示例設置下載屬性。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Anchor download Property 
    </title> 
</head> 
  
<body> 
    <center> 
        <h1>GeeksForGeeks</h1> 
  
        <h2> 
          DOM Anchor download Property 
      </h2> 
  
        <a href= 
  "http://www.example.com:4097/test.htm#part2" 
           id="GFG"
           rel="nofollow"
           hreflang="en-us" 
           target="_self" 
           download="GFG_Logo"> 
            
            <img src= 
"https://media.geeksforgeeks.org/wp-content/uploads/gfg1-11.png" 
                 width="300" 
                 height="250"> 
        </a> 
  
        <BR> 
        <button onclick="myGeeks()"> 
          Submit 
      </button> 
  
        <p id="sudo" 
           style="color:green; 
                  font-size:25px;"> 
      </p> 
  
        <script> 
            function myGeeks() { 
                var x =  
                    document.getElementById( 
                      "GFG").download = "LOGO OF Geeks"; 
                
                document.getElementById( 
                  "sudo").innerHTML =  
                  "The value of the download attribute "+ 
                  "was changed to " + x; 
            } 
        </script> " 
    </center> 
</body> 
  
</html>

輸出:
單擊“ OM”按鈕之前:

單擊按鈕後:

支持的瀏覽器:下麵列出了DOM Anchor下載屬性支持的瀏覽器:

  • 穀歌瀏覽器
  • Internet Explorer 10.0以上
  • Firefox
  • Opera
  • Safari


相關用法


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