JavaScript Tutorial/jQuery/is
is() can be used inside an event handler.
<source lang="javascript">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("div").one("click", function () { if ($(this).is(":first-child")) { $("p").text(":first-child"); } else if ($(this).is(".blue,.red")) { $("p").text("It"s a blue or red div."); } else if ($(this).is(":contains("asdf")")) { $("p").text("It"s asdf!"); } else { $("p").html("It"s nothing special."); } }); }); </script> <style> div.middle { color: red; }
</style>
</head> <body> <body>
asdf
asdf
asdf
asdfasdf
asdf
asdf
</body>
</html></source>
is(expr)
<source lang="javascript">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("div").one("click", function () { if ($(this).is(":first-child")) { $("p").text(":first-child"); } else if ($(this).is(".blue,.red")) { $("p").text("It"s a blue or red div."); } else if ($(this).is(":contains("asdf")")) { $("p").text("It"s asdf!"); } else { $("p").html("It"s nothing special."); } }); }); </script> <style> div.middle { color: red; }
</style>
</head> <body> <body>
asdf
asdf
asdf
asdfasdf
asdf
asdf
</body>
</html></source>
Is it class
<source lang="javascript">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("div").one("click", function () { if ($(this).is(".blue,.red")) { $("p").text("It"s the blue and red div."); }else{ $("p").text("It"s NOT the blue and red div."); } $("p").hide().slideDown("slow"); }); }); </script> </head> <body> <body> Press each to see the text.
asdf
asdf
asdf
asdf
asdf
asdf
</body>
</html></source>
Traversing IS
<source lang="javascript">
<html>
<head> <script type="text/javascript" src="js/jquery-1.3.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ var isFormParent = $("input[type="checkbox"]").parent().is("form") alert(isFormParent); }); </script> </head> <body> <body> <form><input type="checkbox" /></form>
</body>
</html></source>