An ultra simple example of jQuery callback function of sharing

  • 2021-07-09 06:59:19
  • OfStack

jQuery callback function is simple to use

For example, if we want to trigger an event after clicking a button,

Hide some specified contents first,

Then pop up the relevant information dialog box.

If you use the common method,

Without the callback function,

What effect will it have?

The effect is to pop up a dialog box before hiding the contents.

Then hide the specified content.

This is obviously not the effect we want.

If you use callback functions, you can solve this problem.

Of course, the callback function is far more than that...

The specific code is as follows:


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
  String path = request.getContextPath(); 
  String basePath = request.getScheme() + "://" 
      + request.getServerName() + ":" + request.getServerPort() 
      + path + "/"; 
%> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
  <head> 
    <base href="<%=basePath%>"> 
 
    <title>My JSP 'MyJsp.jsp' starting page</title> 
    <title>test</title> 
    <script type="text/javascript" src="js/jQuery/jquery-1.4.4.min.js"></script> 
    <script type="text/javascript"> 
      $(document).ready(function(){ 
        $("#button1").click(function(){ 
          $("p").hide("slow"); 
           alert(" Do not use callback, pop up dialog box before hiding! "); 
        }) 
        $("#button2").click(function(){ 
          $("p").hide("slow",function(){ 
            alert(" Use the callback function to hide before popping up the dialog box !"); 
          });     
        }) 
      }) 
    </script> 
  </head> 
  <body> 
    <p> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
      <br> 
      I love you,java. 
    </p> 
    <input id="button1" type="button" value=" No callback function used !" /> 
    <input id="button2" type="button" value=" Use callback functions !" /> 
  </body> 
</html> 

Related articles: