JQuery调用绑定click事件的3种写法

第一种方式:

$(document).ready(function(){ $("#clickme").click(function(){ alert("Hello World click"); });

第二种方式:

$('#clickmebind').bind("click", function(){ alert("Hello World bind"); });

第三种方式:

$('#clickmeon').on('click', function(){ alert("Hello World on"); }); });

注意:第三种方式只适用于jquery 1.7以上的版本
源码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <script type="text/javascript" src="https://www.jb51.net/js/jquery-1.6.1.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/js/jquery.validate.js"></script> <script type="text/javascript" src="https://www.jb51.net/js/jquery.metadata.js"></script> <script type="text/javascript" src="https://www.jb51.net/js/messages_zh.js"></script> <style type="text/css"> #frm label.error { color: Red; } </style> </head> <script type="text/javascript"> $(document).ready(function(){ $("#clickme").click(function(){ alert("Hello World click"); }); $('#clickmebind').bind("click", function(){ alert("Hello World bind"); }); $('#clickmeon').on('click', function(){ alert("Hello World on"); }); }); </script> <body> <label></label> <form method="post" action=""><label>用 户 名: <input type="text" /> </label> <p> <label>邮 编 :<label></label></label> <label> <input type="text" /> <br /> </label> </p> <p><label>数 字 : <input type="text" /> </label> <br /><label>身份证号: <input type="text" /> </label> &nbsp; <label> <input type="button" value="click me" /> </label> <label> <input type="button" value="clickme_bind" /> </label> <label> <input type="button" value="clickme_on" /> </label> </p> </form> </body> </html>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgjpjs.html