一添加點擊事件javascript
<!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> <script src="../jquery-3.3.1.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <input type="button" class="cc" value="ff"/> <input type="button" class="cc" value="gg"/> <input type="button" class="cc" value="ii"/> <input type="button" name="cc" value="jj"/> </body> <script type="text/javascript"> var a = $("#aa"); var b = $(".bb"); var c = $(".cc"); $("[name='cc']").click(function(){ alert("nihao"); //單個添加單擊事件 }) $(".cc").on("click",function(){ alert($(this).val());//誰觸發。this就表明誰 })//多個元素單擊加事件 </script> </html>
2、掛事件php
<!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> <script src="../jquery-3.3.1.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <input type="button" class="cc" value="掛事件" id="a"/> <input type="button" class="cc" value="測試事件" id="b"/> <input type="button" class="cc" value="移除事件" id="c"/> </body> <script type="text/javascript"> var a = $("#aa"); var b = $(".bb"); var c = $(".cc"); $("#a").click(function(){ $("#b").bind("click",function(){ alert("測試事件"); }) })//掛事件 </script> </html>
顯示:html
三 移除事件java
<!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> <script src="../jquery-3.3.1.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <input type="button" class="cc" value="掛事件" id="a"/> <input type="button" class="cc" value="測試事件" id="b"/> <input type="button" class="cc" value="移除事件" id="c"/> </body> <script type="text/javascript"> $("#a").click(function(){ $("#b").bind("click",function(){ alert("測試實驗"); }); });//掛事件 $("#c").click(function(){ $("#b").unbind("click"); });//移除事件 </script> </html>