首页 > 代码库 > 绑定事件

绑定事件

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="http://www.mamicode.com/js/jquery-1.12.1.js" type="text/javascript"></script>
<script type="text/javascript">


function testEvent(){
$("div.gray").on("click",function(){
var self = this;
alert("clicked");
/*$("div.red").on("dblclick",function(){
var $self = this;
console.log("123");
$(self).off("click");
})*/
})

$("div.gray").on("sxt.test",function(){
alert("sxt.tested");
});
}

function testOffEvent(){

}

function testTrigger(){
//$("div.gray").trigger("click");
$("div.gray").trigger("sxt.test");
}

/*$(function(){
$("div.gray").on("dblclick",function(){
alert("clicked");
})
})*/

 

function testBind(){
/* $("div.red").bind("click mouseover",function(){
alert("clicked");
});*/

$("div.red").bind({
"click":function(){
alert("clicked");
},
"mouseover mouseout":function(){
alert("mouseovered");
}
});
}


</script>
<style type="text/css">

.box{
width: 200px;
height: 200px;
border: 1px solid green;
border-radius: 5px;
display: inline-block;
margin: 5px;
}
.gray{
background-color: gray;
}

.red{
background-color: red;
}

</style>
</head>
<body>
<input type="button" value="http://www.mamicode.com/bindEvent" onclick="testEvent()">
<input type="button" value="http://www.mamicode.com/trigger" onclick="testTrigger()">
<input type="button" value="http://www.mamicode.com/testBind" onclick="testBind()">
<hr>
<div>
<div class="box gray"> box1 </div>
<div class="box red"> box2 </div>
</div>
</body>
</html>

绑定事件