jQuery delegate()
attach slide event
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/*from ww w. ja va2 s . c om*/ <script> $(document).ready(function(){ $("div").delegate("p", "click", function(){ $(this).slideToggle(); }); $("button").click(function(){ $("<p>This is a new paragraph.</p>").insertAfter("button"); }); }); </script> </head> <body> <div style="background-color:yellow"> <p>This is a paragraph.</p> <p>Click any p element to make it disappear. </p> <button>Insert a new p element</button> </div> </body> </html>