简体   繁体   English

当我在浏览器中单击另存为 HTML 时,如何下载 web 页面的部分内容而不是所有内容

[英]How can i download some of content instead of all content of web page when i click on Save as HTML in browser

When I click on save as HTML button its Download all content of web page but i have require to Download some for specific content from all content.当我单击另存为 HTML 按钮时,它会下载 web 页面的所有内容,但我需要从所有内容中下载一些特定内容。

 function download(filename, contents) { const anchor = document.createElement('a'); anchor.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(contents)); anchor.setAttribute('download', filename); anchor.style.display = 'none'; document.body.appendChild(anchor); anchor.click(); document.body.removeChild(anchor); } const btn = document.querySelector('#save-btn'); btn.addEventListener('click', (evt) => { const container = document.createElement('div'); const html = document.createElement('html'); html.innerHTML = document.documentElement.innerHTML; container.appendChild(html); download('index.html', container.innerHTML); });
 <p>Hello World</p> <button id="save-btn">Save as HTML</button> <div> <p>Hello World-1</p> <p>Hello World-2</p> <p>Hello World-3</p> </div>

On above HTML Example, when i click on save as HTML Button i have download some of portion of file like download div content instead of all content.在上面的 HTML 示例中,当我单击另存为 HTML 按钮时,我下载了部分文件,例如下载 div 内容而不是所有内容。 in sort when I open download file it display only this content:当我打开下载文件时,它只显示以下内容:

<div>
  <p>Hello World-1</p>
  <p>Hello World-2</p>
  <p>Hello World-3</p>
</div>

To only download the HTML of a specific element, change the logic to select that element instead of the entire body, like this:要仅下载特定元素的 HTML,请将逻辑更改为该元素而不是整个主体的 select,如下所示:

document.querySelector('#save-btn').addEventListener('click', e => {
  e.preventDefault();
  let html = document.querySelector('div').outerHTML; // update this selector in your local version    
  download('index.html', html);
});

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);
  element.style.display = 'none';
  document.body.appendChild(element);
  
  element.click();
  document.body.removeChild(element);
}

Working example工作示例

暂无
暂无

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

相关问题 当我单击另存为 HTML 而不是右键单击浏览器并保存时,如何将 React 单页保存为 HTML - how can i save React single page as HTML when i click on Save as HTML instead for right click on Browser and save it 如何在dowllaods文件夹中而不是在浏览器中使用html2canvas和FileSaver下载div的内容? - How can I download content of div using html2canvas and FileSaver in the dowllaods folder instead of in the browser? 从某些不同的页面链接返回时,如何编译html内容? - How can I compile my html content when coming back from some different page links? 浏览器导航到文件下载时如何显示页面内容? - How do I display page content when browser navigates to an file download? 如何通过单击链接在 Angular Front End Web App 的浏览器中下载或保存 .csv 文件? 浏览器投诉没有文件 - How can I download or save a .csv file in browser in Angular Front End Web App on click of a link? browser complaints no file 如何保存编辑器的内容,而不是整个HTML页面的内容? - How do I save the content of the editor, not the whole HTML page? 当我单击按钮时,我在html中有textarea,我需要将textarea内的内容另存为pdf文件。 我该怎么做? - im having textarea in html when i click the button i need to save the content inside the textarea as pdf file. how can i do it? 当他们在浏览器中单击“上一步”时,如何防止内容重新加载? - How can I prevent the content from being reloaded when they click Back on the browser? 如何下载/保存用PHP生成的HTML页面的内容? - How to download / save the content of a HTML page generated with PHP? 如何让html的内容占据整个浏览器window? - How can I make the html content to occupy the entire browser window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM