jQuery 中的 bind( type, data, fn )方

首页 / jQuery入门教程 / jQuery 中的 bind( type, data, fn )方

bind(type,[data],fn)方法将处理程序绑定到每个匹配元素的一个或多个事件(如click)。也可以绑定自定义事件。

bind( type, [data], fn ) - 语法

selector.bind( type, [data], fn )

这是此方法使用的所有参数的描述-

  • type      -  一种或多种事件类型,以空格分隔。

  • data     -  这是可选参数,表示作为event.data传递到事件处理程序的其他数据。

  • fn         -  绑定到每个匹配元素上的事件的函数。

bind( type, [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>

这将产生以下输出-

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Java核心技术面试精讲 -〔杨晓峰〕

面试现场 -〔白海飞〕

消息队列高手课 -〔李玥〕

安全攻防技能30讲 -〔何为舟〕

Linux内核技术实战课 -〔邵亚方〕

大厂晋升指南 -〔李运华〕

恋爱必修课 -〔李一帆〕

郭东白的架构课 -〔郭东白〕

结构沟通力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)