Hongmu Notes
Home Language Notes After jQuery clicks on the pre tag, it saves the content in the pre tag as a file and downloads it.
Language Notes JavaScript

After jQuery clicks on the pre tag, it saves the content in the pre tag as a file and downloads it.

After jQuery clicks on the pre tag, it saves the content in the pre tag as a file and downloads it.

This code is a JavaScript/jQuery function. The main function is to click on the page after the page is loaded. <pre> element, its text content is downloaded locally in the specified file type.

The specific code analysis is as follows:

  1. First use $(document).ready() method to ensure that the code is executed after the document is loaded.
  2. Use $() Function selector selects all <pre> element and use .click() Method to bind a click event handler to it.
  3. When the user clicks <pre> element, triggers the click event handler.
  4. In the click event handler, first use $(this).text() Get the currently clicked <pre> The text content of the element and assigned to the variable content
  5. Then use $(this).attr('data-filename') Get the currently clicked <pre> elemental data-filename attribute value and assign it to a variable fileName. This attribute is used to specify the name of the downloaded file.
  6. The call is called downloadFile function, pass in contenttext/plainfileName as parameters.
  7. downloadFile The function is defined inside an outer function and accepts three parameters:content(Document Content),contentType(File type) and fileName(filename).
  8. downloadFile Use within a function new Blob() Create a Blob object whose content is: contentType: contentType
  9. Create one <a> Element; configure it. href Attribute: window.URL.createObjectURL(blob)This will generate a temporary URL for the Blob object.
  10. set up <a> elemental download Attribute: fileNameThe name of the file to download.
  11. <a> Element style settings: display: none;Hide this element.
  12. <a> Add elements to the document <body> Element.
  13. Call .click() Method: Simulating user clicks <a> Element: enables file download.
  14. After completing the file download, please use it. document.body.removeChild(link)<a> The element is removed from the document.

Overall, this code implements a simple file download functionality; simply click on the button on the page. <pre> When an element is encountered, its content will be automatically downloaded to the local device in the specified file format; the file name is determined by data-filename Attribute specification.

Code:

$(document).ready(function() {
  $('pre').click(function() {
    var content = $(this).text();
    var fileName = $(this).attr('data-filename');
    downloadFile(content, 'text/plain', fileName);
  });

  function downloadFile(content, contentType, fileName) {
    var blob = new Blob([content], {type: contentType});

    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = fileName;
    link.style.display = 'none';

    document.body.appendChild(link);
    link.click();

    document.body.removeChild(link);
  }
});

Second version:

Double-click to determine whether to download the pop-up file; click 'Download' to proceed with the download!

$(document).ready(function() {
  $('pre').dblclick(function() {
    var content = $(this).text();
    var fileName = $(this).attr('data-filename');
    showDownloadConfirmation(content, 'text/plain', fileName);
  });

  function showDownloadConfirmation(content, contentType, fileName) {
    if (confirm("是否下载该文件?")) {
      downloadFile(content, contentType, fileName);
    }
  }

  function downloadFile(content, contentType, fileName) {
    var blob = new Blob([content], {type: contentType});

    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = fileName;
    link.style.display = 'none';

    document.body.appendChild(link);
    link.click();

    document.body.removeChild(link);
  }
});

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

(PC + WAP) PbootCMS website template for higher education institutions, colleges, and vocational schools; Download source code for government agencies and associations websites – 0130

(PC + WAP) PbootCMS website template for higher education institutions, colleges, and vocational schools; Download source code for government agencies and associations websites – 0130 Practical Collection pbootcms Template

A PbootCMS template designed for university websites, vocational school websites, and government association websites, supporting both PC and WAP devices. Its professional and sophisticated design effectively showcases the institution's identity, teaching achievements, and organizational functions. It is an ideal choice for universities or government agencies looking to create official portal websites. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: ww...
👁 44
Responsive Smart Bathroom Product Website Template 0929

Responsive Smart Bathroom Product Website Template 0929 Practical Collection Yiyou template

An EyouCMS responsive website template designed for the smart bathroom products industry. Its modern, tech-driven design is ideal for showcasing bathroom products, smart systems, spatial designs, and brand identity. This template helps bathroom brands showcase their products online and attract customers in the home decoration and construction sectors. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Questions for EyouCMS | EyouCMS...
👁 35
(Adaptive Mobile Version) Responsive PBootCMS Air Conditioning Repair Service Website Template – Download Website Source Code for Air Conditioning Installation & Repair Services – 0776

(Adaptive Mobile Version) Responsive PBootCMS Air Conditioning Repair Service Website Template – Download Website Source Code for Air Conditioning Installation & Repair Services – 0776 Practical Collection pbootcms Template

A responsive HVAC maintenance service website template built with PbootCMS, compatible with both PC and WAP devices. The professional and practical design is ideal for HVAC installation, repair, and maintenance service providers to showcase their business offerings. It helps home appliance service companies attract local customers online and enhance their brand awareness. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 34
mysqli_query() expects parameter 1 to be mysqli, null given in

mysqli_query() expects parameter 1 to be mysqli, null given in Summary of pitfalls

This error usually indicates that you failed to establish a successful connection to the MySQL database, or that you closed the connection before calling the mysqli_query() function. Below are some possible causes of this error and corresponding solutions: Failed connection to the MySQL database: Please ensure that you have correctly configured the mysqli_connect() function to connect to the MySQL database, and that the database server...
👁 220
Building Materials & Construction/Interior Decoration Website Templates – 0255

Building Materials & Construction/Interior Decoration Website Templates – 0255 Practical Collection Yiyou template

This EyouCMS template is ideal for the building materials, construction, and interior decoration industries. Its modern and professional design style effectively showcases building material products, construction projects, decoration solutions, and corporate strength. It helps building material and construction enterprises showcase their brands online and attract developers and property owners. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 38
(PC + WAP) Black plastic sheet website template – pbootcms website template; PC plastic sheet website source code download – 0131

(PC + WAP) Black plastic sheet website template – pbootcms website template; PC plastic sheet website source code download – 0131 Practical Collection pbootcms Template

This PbootCMS template is designed for the plastic sheet and PC sheet industries and supports access via both PC and WAP devices. Its sophisticated and professional design effectively showcases the characteristics of plastic sheet products and their engineering applications. It enables plastic sheet manufacturers to showcase their product specifications and capabilities online, helping them attract customers from the industrial and construction sectors. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password:...
👁 38