当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。