事件绑定
- 匿名函数
$('#btn').click(function(){
console.log(666);
});
- 命名函数
$('#btn').click(test);
function test(){
console.log(666);
}
- .bind()
$('#dv').bind('onmouseover',function(){//参数一:事件类型,参数二:响应事件函数
$(this).css({//使用对象一次性修改多个样式
'width':'300px',
'height':'300px',
'border':'1px solid #000000'
});
});
事件解绑
- .unbind()
$('#dv').unbind('mouseover');
触发事件
使用js触发元素点击事件
$('#dv').trigger('click');
阻止事件冒泡
$('div').click(function(e){
$(this).css('background-color','#000000');
//方法一
//e.stopPropagation();
//方法二
return false;
});
阻止默认行为
$('a').click(function(e){
alert('asdf');
//方法一
//e.preventDefault();
//方法二
return false;
});