1 事件绑定的语法
1方式
$obj.bind(“事件名称”,事件处理函数);
ex:
$obj.bind(“click”,function(){
//事件的操作
//允许使用this来引用处理事件的DOM元素
})
2 方式2(常用)
$obj.事件名称(事件处理函数);
ex:
$obj.click(function(){
//事件的操作
});
注意:
$obj.click(),表示要触发$obj元素click事件
<script src="../jquery-1.11.3.js"></script> <script> $(function(){ $("#btn1").bind("click",function(){ alert("使用bind绑定事件"); }); $("#btn2").click(function(){ alert("使用事件名称"); }); }); </script> <button id ="btn1">使用bind绑定事件</button> <button id ="btn2">使用事件名称</button> |
事件对象
1$obj.bind(“事件名称”,function(event){
//逻辑处理
});
2 $obj.事件名称(function(event){
//逻辑处理
});