bind(type,[data],fn)方法将处理程序绑定到每个匹配元素的一个或多个事件(如click)。也可以绑定自定义事件。
selector.bind( type, [data], fn )
这是此方法使用的所有参数的描述-
type - 一种或多种事件类型,以空格分隔。
data - 这是可选参数,表示作为event.data传递到事件处理程序的其他数据。
fn - 绑定到每个匹配元素上的事件的函数。
<html> <head> <title>The jQuery Example</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $('div').bind('click', function( event ){ alert('Hi there!'); }); }); </script> <style> .div{ margin:10px;padding:12px; border:2px solid #666; width:60px;} </style> </head> <body> <p>Click on any square below to see the result:</p> <div class="div" style="background-color:blue;"></div> <div class="div" style="background-color:green;"></div> <div class="div" style="background-color:red;"></div> </body> </html>
这将产生以下输出-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)