Switch class when clicking the div tag
<html>
<head>
<script type='text/javascript' src='js/jquery-1.3.2.js'></script>
<script type='text/javascript'>
$(document).bind(
'ready',
function() {
$('div').bind(
'mouseover',
function() {
$(this).addClass('myMouseOver');
}
);
$('div').bind(
'mouseout',
function() {
$(this).removeClass('myMouseOver');
}
);
$('div').bind(
'click',
function() {
if ($(this).hasClass('myMouseOn')) {
$(this).removeClass('myMouseOn');
} else {
$(this).addClass('myMouseOn');
}
}
);
}
);
</script>
<style type='text/css'>
div {
border: 1px solid rgb(200, 200, 200);
width: 10px;
height: 10px;
margin: 5px;
float: left;
}
div.myMouseOver {
background: red;
}
div.myMouseOn {
background: yellow;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
Related examples in the same category