From jQuery JavaScript Library
« Back to Traversing
hasClass( class )
Checks the current selection against a class and returns true, if at least one element of the selection has the given class.
This is an alternative to is("." + class).
Arguments:| class | String | |
|---|
| The class to match. |
Examples:| Name | Type |
Check to see if an element has a specific class, and if so, perform an action.
$("div").click(function(){
if ( $(this).hasClass("protected") )
$(this).animate({ left: -10 }, 75)
.animate({ left: 10 }, 75)
.animate({ left: -10 }, 75)
.animate({ left: 10 }, 75)
.animate({ left: 0 }, 75);
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="../jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div").click(function(){
if ( $(this).hasClass("protected") )
$(this).animate({ left: -10 }, 75)
.animate({ left: 10 }, 75)
.animate({ left: -10 }, 75)
.animate({ left: 10 }, 75)
.animate({ left: 0 }, 75);
});
});
</script>
<style>
div { width: 80px; height: 80px; background: #abc;
position: relative; border: 2px solid black;
margin: 20px 0px; float: left; left:0 }
div.protected { border-color: red; }
span { display:block; float:left; width:20px;
height:20px; }
</style>
</head>
<body>
<span></span><div class="protected"></div>
<span></span><div></div>
<span></span><div></div>
<span></span><div class="protected"></div>
</body>
</html>