.is()
Syntax
.is(selector)
Parameters
selector
- A string containing a selector expression to match elements against
Return value
A Boolean indicating whether an element matches the selector.
Description
Check the current matched set of elements against a selector and return true if at least one of these elements matches the selector.
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.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>
<div></div>
</body>
</html>
Is it a style I need.
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.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.
<div class=blue>asdf</div>
<div class=red>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<p></p>
</body>
</html>
is(expr)
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.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 <em>special</em>.");
}
});
});
</script>
<style>
div.middle { color: red; }
</style>
</head>
<body>
<body>
<div>asdf</div>
<div class="blue">asdf</div>
<div class="red">asdf</div>
<div><span>asdf</span>asdf</div>
<div class="blue">asdf</div>
<p>asdf</p>
</body>
</html>
is() can be used inside an event handler.
<html>
<head>
<script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.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 <em>special</em>.");
}
});
});
</script>
<style>
div.middle { color: red; }
</style>
</head>
<body>
<body>
<div>asdf</div>
<div class="blue">asdf</div>
<div class="red">asdf</div>
<div><span>asdf</span>asdf</div>
<div class="blue">asdf</div>
<p>asdf</p>
</body>
</html>
Home
JavaScript Book
jQuery
JavaScript Book
jQuery
DOM:
- jQuery DOM
- $("html tags"):generate code with the jQuery wrapper function.
- .add()
- .addClass()
- .after()
- .andSelf()
- .append()
- .appendTo()
- .attr()
- .before()
- .children()
- .clone()
- .closest()
- .contents()
- .css()
- .detach()
- .filter()
- .first()
- .get()
- .has()
- .hasClass()
- .height()
- .html()
- .index()
- .innerHeight()
- .innerWidth()
- .insertAfter()
- .insertBefore()
- .is()
- .last()
- .map()
- .next()
- .nextAll()
- .nextUntil()
- .not()
- .offset()
- .offsetParent()
- .outerHeight()
- .outerWidth()
- .parent()
- .parents()
- .parentsUntil()
- .position()
- .prepend()
- .prependTo()
- .prev()
- .prevAll()
- .prevUntil()
- .remove()
- .removeClass()
- .removeAttr()
- .replaceAll()
- .replaceWith()
- .siblings()
- .scrollLeft()
- .scrollTop()
- .slice()
- .text()
- .toArray()
- .toggleClass()
- .unwrap()
- .val()
- .wrap()
- .wrapAll()
- .wrapInner()
- .width()