JQuery simple implementation code for custom events

  • 2020-03-30 01:34:48
  • OfStack

One, demand reasons
Although JQuery defines a lot of events for us, sometimes they don't meet our needs, so this example implements a custom event implementation.

Two, concrete implementation


<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title> Custom event </title>
<script language="JavaScript"src="../jQuery/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
     $(function(){
         $("#btn").bind("myClick",function(){
              $("#test").append(" Custom event ").append($("<br>"));
         });
         $("#btn").click(function(){
              $(this).trigger("myClick");
         });
     });
</script>
</head>
<body>
     <input id="btn" type="button" value=" Am I ">
     <div id="test"></div>
</body>
</html>


Related articles: