jQuery 是为事件处理特别设计的。
jQuery 事件处理方法是 jQuery 中的核心函数。
事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。
通常会把 jQuery 代码放到 <head> 部分的事件处理方法中:
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); </script> </head> <body> <h2>This is a heading</h2> <p>This is a paragraph.</p> <p>This is another paragraph.</p> <button>Click me</button> </body> </html>注释:当按钮的点击事件被触发时会调用一个函数,该方法隐藏所有 <p> 元素。
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="my_jquery_functions.js"></script>
<!DOCTYPE html> <html> <head> <script src="../jquery/jquery-1.11.1.min.js"></script> <script> $.noConflict(); jQuery(document).ready(function(){ jQuery("button").click(function(){ jQuery("p").text("jQuery 仍在运行!"); }); }); </script> </head> <body> <p>这是一个段落。</p> <button>测试 jQuery</button> </body> </html>
评论