源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.net/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#div1").on("click","p",function(){ $(this).css("background-color","pink"); }); $("#div2").delegate("p","click",function(){ $(this).css("background-color","pink"); }); }); </script> </head> <body> <h4 style="color:green;">该实例演示了如何使用 on() 来达到与 delegate() 相同的效果。.</h4> <div id="div1"> <p>点我使用 <b>on() 方法设置背景颜色</b>。</p> </div> <div id="div2"> <p>点我使用 <b>delegate() 方法设置背景颜色</b>。</p> </div> </body> </html>
运行结果: