Bind method is used for attaching an event handler to a document.As shown below click,double click,mouse enter, mouse leave are event handlers directly attached to the document.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>bind demo</title>
<style>
p {
background: yellow;
font-weight: bold;
cursor: pointer;
padding: 5px;
}
p.over {
background: #ccc;
}
span {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>Click or double click here.</p>
<span></span>
<div></div>
<script>
$(document).ready(function(){
$("p").bind("click",function(event){
$("span").text("click happened" );
});
$("p").bind("dblclick",function(event){
$("span").text("double click happened");
});
$("p").bind("mouseenter mouseleave",function(event){
$(this).toggleClass("over");
});
});
</script>
</body>
</html>
0 Comment(s)