jQuery事件

1. 鼠标事件

  1. click dblclick(同时会触发单击事件)
$('a').click(function(){
    $('img').eq($(this).index()).css({'opacity':'1'})
        .siblings().css({'opacity':'0'});
});
//双击
$('a').dblclick(function(){
    $('img').eq($(this).index()).css({'opacity':'1'})
        .siblings().css({'opacity':'0'});
});
  1. mousedown mouseup
  2. mouseenter mouseleave
  3. hover(mouseenter+mouseleave) 接受两个函数
$('a').hover(function(){
    $('img').eq($(this).index()).css({'opacity':'1'})
        .siblings().css({'opacity':'0'});},function(){
    $('img').eq($(this).index()).css({'opacity':'0'})
        .siblings().css({'opacity':'1'}); 
    });
  1. mouseover mouseout(进出子元素时也触发 作用跟enter和leave一样)
  2. mousemove 在内部发生移动
  3. scroll(滚动指定元素时)

2. 键盘事件

  1. keydown keyup keypress
//keydown只能发生在document下
$(document).keydown(function(){});
$('input').keyup(function(){});
//keypress只有在正常输入时才有效 输入法、删除、回车等无效
$(document).keypress(function(){});
$('input').keypress(function(){});

3. 其他事件

  1. ready DOM载入就绪后执行
  2. resize 调整浏览器窗口大小时发生
//浏览器 得用window
$(window).resize(function(){});
  1. focus blur
  2. change
  3. select input类型为text或textarea等中的文本被选中时
  4. submit 提交表单时(button用在form中有提交表单功能,用在其他地方没有)
//提交表单
$('input[type=button]').click(function(){
	$('form').submit();
});
//阻止表单提交
$('form').submit(function(){
	return false;//只要return false就不提交
});
//表单验证等
$('form').submit(function(){
	var value=$('input[type=text]').val();
	if(value){
		return true;
	}

4. 事件参数

  1. event 任何事件都有
//左右键控制图片轮播
var index=0;
$('a').mouseenter(function(){
	index=$(this).index();
	swiper();
}
$(document).keydown(function(event){
	if(event.keyCode == 37){
		index = index>0? --index:$('a').length-1;
	}else if(event.keyCode == 39){
		index = index<$('a').length-1? ++index:0;
	}else{return true;)
	swiper();	
});
var swiper = function(){
	$('img').eq(index).css({'opacity':'1'})
        .siblings().css({'opacity':'0'});
}

5. 事件绑定与取消

  1. on 绑定多个事件
$('input').keyup(function(){});
$('input').on('keyup',function(){});
$(document).on('keyup','a',function(event){
	event.stopPropagation();
});
//绑定多个事件
$('a').add(document).on({
	mouseenter:function(event){},
	keydown:function(event){}
});
  1. off
$(document).off('click','.obj',funcName);
  1. one 绑定一个一次性的事件函数
$(document).one('click','.obj',funcName);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值