Home  >  Article  >  Web Front-end  >  How to use jQuery in preview scene

How to use jQuery in preview scene

PHPz
PHPzOriginal
2023-04-17 15:00:36490browse

In web development, previewing images, audio or videos is a very common requirement. Of course, you'll need to use the new features of HTML5 or a Flash plug-in to accomplish this task. However, you may have also encountered a problem: how to refresh or clear the media files in the preview scene?

This is a very useful feature of jQuery that we need to understand: clearing/removing preview content. Let's learn how to use jQuery in the preview scene.

Preview function implementation

First, we need an HTML element that can be previewed, such as img, audio or video. Usually, we use HTML5's <input type="file"> element to achieve this function, and display its content in the preview element after selecting the media file.

<input type="file" id="mediaFile">
<img id="preview">

Next, use jQuery to listen to the file selection event and read the selected file as a URL and set it as the src attribute of the preview element.

$('#mediaFile').change(function() {
  var file = this.files[0];
  var reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = function(e) {
    $('#preview').attr('src', e.target.result);
  }
});

Here we use the FileReader object to read the file content as DataURL. We then set this DataURL as the src attribute of the preview element. This will display a preview of the media file.

Clear preview function implementation

After rendering the preview content, if the user wants to replace it or delete it, we need to provide a method to clear the preview content. Let's take a look at how to accomplish this task easily using jQuery.

First, we need to set the src attribute of the preview element to an empty string.

$('#preview').attr('src', '');

This will remove the media content from the preview element. However, if the user selects a file, the file path will still be displayed in the file input element. In order to completely clear the original file selection, we need to use a trick.

We can create a new file input element, replace it with the original input element, and set it to a new, empty file input element. This will clear the file path and reset the file selection.

$('#mediaFile').replaceWith($('#mediaFile').clone(true));

Here, we use jQuery’s clone() method and replaceWith() method to achieve this function. The clone() method will create a copy of the existing element and pass it to the replaceWith() method for replacement.

Complete Example

The following is a complete example that demonstrates how to use jQuery to implement preview and clear preview functions.

<!DOCTYPE html>
<html>
<head>
  <title>jQuery清除预览示例</title>
</head>
<body>
  <input type="file" id="mediaFile">
  <img id="preview">
  
  <button id="clearPreview">清除预览</button>
  
  <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <script>
    $('#mediaFile').change(function() {
      var file = this.files[0];
      var reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = function(e) {
        $('#preview').attr('src', e.target.result);
      }
    });
    
    $('#clearPreview').click(function() {
      $('#preview').attr('src', '');
      $('#mediaFile').replaceWith($('#mediaFile').clone(true));
    });
  </script>
</body>
</html>

Here, we added a button to clear the preview content. Clicking this button will call the clear preview function, set the preview element's src attribute to an empty string, and reset the file selection.

Conclusion

In website or application development, preview function is a very common requirement. Using jQuery tricks, we can easily implement preview and clear preview functionality. Hope this short guide helps!

The above is the detailed content of How to use jQuery in preview scene. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn