jQuery学习5:jQuery事件模型

本文介绍了jQuery中的事件处理机制,包括事件绑定、解除绑定、切换监听器等核心功能,并通过示例展示了如何使用jQuery简化事件处理过程。

jQuery事件模型的功能有:

提供建立事件处理程序的统一方法;

允许在每个元素上为每个时间类型建立多个处理程序;

采用标准的事件类型名称,例如click或mouseover;

使用Event实例可用作处理程序的参数;

对Event实例的最常用的属性进行规范化;

为取消事件和阻塞默认操作提供统一方法。

jQuery绑定事件处理程序:

bind()命令

$('img').bind('click',funciton(event){alert('Hi there');}); 该语句为页面上的图片绑定已提供的内联函数,作为点击事件处理程序。

 

ExpandedBlockStart.gif建立事件处理程序,无需浏览器特定代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  
<head>
    
<title>jQuery Events Example</title>
    <script type="text/javascript" src="../scripts/jquery-1.2.1.js">
    
</script>
    <script type="text/javascript">
      $(
function(){
        $(
'#vstar')
          .bind(
'click',function(event) {
            say(
'Whee once!');
          })
          .bind(
'click',function(event) {
            say(
'Whee twice!');
          })
          .bind(
'click',function(event) {
            say(
'Whee three times!');
          });
      });

      
function say(text) {
        $(
'#console').append('<div>'+text+'</div>');
      }
    
</script>
  </head>

  
<body>
    
<img id="vstar" src="vstar.jpg"/>
    <div id="console"></div>
  </body>
</html>

 

删除事件处理程序unbind(event,listener),unbind(event)

从包装集的所有元素中删除可选的已传递参数所指定的事件处理程序。如果不提供参数,则从元素中删除所有的监听器(即事件处理程序)

起切换作用的监听器toggle()

toggle(listenerOdd,listenerEven)把已传递函数建立为包装集所有元素的一对click事件处理程序,每当触发click事件就相互切换。

ExpandedBlockStart.gif每当点击事件发生时,调用互补的监听器
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  
<head>
    
<title>jQuery Toggle Command Example</title>
    <script type="text/javascript" src="../scripts/jquery-1.2.1.js">
    
</script>
    <script type="text/javascript">
      $(
function(){
        $(
'#vstar').toggle(
          
function(event) {
            $(event.target).css(
'opacity',0.4);
          },
          
function(event) {
            $(event.target).css(
'opacity',1.0);
          }
        );
      });
    
</script>
  </head>

  
<body>
    
<img id="vstar" src="vstar.jpg"/>
  </body>
</html>

 

在元素上方悬停鼠标指针hover(overListener,outListener)建立已匹配元素的mouseover和mouseout事件处理程序。这些处理程序当儿仅当元素所覆盖区域被进入和退出时触发,忽视鼠标指针从父元素到子元素上的迁移

 

ExpandedBlockStart.gif鼠标停留事件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  
<head>
    
<title>Hover example</title>
    <link rel="stylesheet" type="text/css" href="hover.css">
    
<script type="text/javascript"
            src
="../scripts/jquery-1.2.1.js"></script>
    <script type="text/javascript">
      
function report(event) {
        $(
'#console').append('<div>'+event.type+'</div>');
      }

      $(
function(){
        $(
'#outer1')
         .bind(
'mouseover',report)
         .bind(
'mouseout',report);
        $(
'#outer2').hover(report,report);
      });
    
</script>
  </head>

  
<body>
    
<div class="outer" id="outer1">
      Outer 
1
      
<div class="inner" id="inner1">Inner 1</div>
    </div>
    <div class="outer" id="outer2">
      Outer 
2
      
<div class="inner" id="inner2">Inner 2</div>
    </div>
    <div id="console"></div>
  </body>
</html>

 

 

转载于:https://www.cnblogs.com/yangzhijia/archive/2010/01/28/1658454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值