简体   繁体   English

我们如何向其中包含的所有<a>标签</a>添加下载属性 <body></body> 标签(使用JavaScript)?

[英]How can we add a download attribute to all the <a> tags present inside <body></body> tag (using JavaScript)?

Using JavaScript, I want to add a 'download' attribute in all the <a> tags present on my webpage. 我想使用JavaScript在网页上显示的所有<a>标记中添加“下载”属性。

Current code is 当前代码是

<a href="link of some pdf file">View1</a>
<a href="link of some pdf file">View2</a>
<a href="link of some pdf file">View3</a>
<a href="link of some pdf file">View4</a>
<a href="link of some pdf file">View5</a>

What I want is to add download attribute in all the <a> tags at once using JavaScript. 我想要的是使用JavaScript一次在所有<a>标记中添加下载属性。
The result should be same as shown below: 结果应如下所示:

<a href="link of some pdf file" download>View1</a>
<a href="link of some pdf file" download>View2</a>
<a href="link of some pdf file" download>View3</a>
<a href="link of some pdf file" download>View4</a>
<a href="link of some pdf file" download>View5</a>

document.querySelectorAll('a') can get all the <a> element in your document. document.querySelectorAll('a')可以获取文档中的所有<a>元素。

It returns an array so you can use forEach() to iterate through all the elements. 它返回一个数组,因此您可以使用forEach()遍历所有元素。

Finally, you can use setAttribute() to set an element's attribute. 最后,您可以使用setAttribute()设置元素的属性。

You may see <a download=""> but that's the same as <a download> . 您可能会看到<a download="">但与<a download>相同。

See example below. 请参见下面的示例。

 var updateTextarea = function () { document.querySelector('textarea').value = document.querySelector('main').innerHTML; }; var addDownload = function () { document.querySelectorAll('a').forEach(function(e) { e.setAttribute('download', ''); }); updateTextarea(); }; updateTextarea(); 
 <main> <a href="link of some pdf file">View1</a> <a href="link of some pdf file">View2</a> <a href="link of some pdf file">View3</a> <a href="link of some pdf file">View4</a> <a href="link of some pdf file">View5</a> </main> <p><button onclick="addDownload()">Add download</button></p> <p><textarea cols="50" rows="8" readonly></textarea></p> 

There's not really too much to this " download " attribute. 这个“ download ”属性实际上并没有太多。 You simply provide a filename as the attribute value . 您只需提供一个文件名作为属性值 And then, when the user clicks on the anchor link, they will download the HREF location and save the resultant payload using the provided filename: 然后,当用户单击anchor link,他们将下载HREF位置并使用提供的文件名保存生成的有效负载:

<a href="./generate-zip?id=4" download="assets.zip">Download</a>

In this case, the server-generated ZIP file will be saved to the user's computer as "assets.zip". 在这种情况下,服务器生成的ZIP file将作为"assets.zip".保存到用户的计算机中"assets.zip".

for further details you can visit 有关更多详细信息,您可以访问

https://www.bennadel.com/blog/3412-using-the-anchor-tag-href-and-download-attributes-to-force-a-file-download.htm https://www.bennadel.com/blog/3412-using-the-anchor-tag-href-and-download-attributes-to-force-a-file-download.htm

hope that would help you to sort out problem 希望能帮助您解决问题

 var linkTags = document.getElementsByTagName('a'); for(var i= 0; i< linkTags.length; i++) linkTags[i].setAttribute('download',''); 
 <a href="link of some pdf file">View1</a> <a href="link of some pdf file">View2</a> <a href="link of some pdf file">View3</a> <a href="link of some pdf file">View4</a> <a href="link of some pdf file">View5</a> 

You should use the download attribute. 您应该使用下载属性。

Your tags will look like this 您的标签将如下所示

<a href="link of some pdf file" download = "link_of_some_pdf_file.pdf">View1</a>

You should do this 你应该做这个

element = document.getElementById(...);
element.setAttribute('download', element.getAttribute('href');

I'll let you figure out how to put it in a cycle ;) 我会让您弄清楚如何将其循环放置;)

 var hrefs = document.getElementsByTagName("a"); for (var i=0, max=hrefs.length; i < max; i++) { hrefs[i].setAttribute("download","pdf"); } 
 <a href="link of some pdf file">View1</a> <a href="link of some pdf file">View2</a> <a href="link of some pdf file">View3</a> <a href="link of some pdf file">View4</a> <a href="link of some pdf file">View5</a> 

the first thing is to get all the elements from the dom, 首先是从dom中获取所有元素,

the traverse each a element and add attribute with setAttribute function 遍历每个元素并使用setAttribute函数添加属性

You need to get All elements a , then loop through it set attribute of each element. 您需要获取所有元素a ,然后遍历每个元素的set属性。

 var links = document.getElementsByTagName('a'); for(var i= 0; i< links.length; i++) links[i].setAttribute('download',''); 
 <a href="link of some pdf file">View1</a> <a href="link of some pdf file">View2</a> <a href="link of some pdf file">View3</a> <a href="link of some pdf file">View4</a> <a href="link of some pdf file">View5</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在外部主体标签中添加元素并排除 iframe 主体标签 - how to add an element in the outer body tag and exclude iframe body tags 如何在Body标签内调用Javascript文件 - How to call the Javascript file inside Body tag 如何在里面添加某些脚本标签<head>和<body>使用 HtmlWebackPlugin 时的标签 - How do I add certain script tags inside <head> and <body> tags when using HtmlWebackPlugin 如何使用 javascript 或 jquery 在另一个标签中添加一个标签属性? - How to add one tags attribute in another tag using javascript or jquery? 如何在Javascript的消息正文中添加按钮? - How to add a button inside message body in Javascript? 如何在iframe主体内添加javascript函数 - How to add a javascript function inside the body of iframe 如何使用javascript获取body标签的值? - How to get the value of a body tag using javascript? 如何或可以使用JavaScript在html或body标签后附加div标签? - How or can I use JavaScript to append a div tag after the html or body tags? 添加文字到 <p> 在javascript中使用body标签的ID进行标记 - Add text to <p> tag by using ID of body tag in javascript 如何使用JavaScript使用HTML从字符串中删除整个HTML,HEAD标签和BODY标签? - How to remove whole HTML, HEAD tags and BODY tag from string with HTML using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM