« Обратно к списку traversing-функций
is( выражение )
Проверяет текущий набор элементов на соответствие указанному выражению и возвращает true, если хотя бы один элемент соответствует выражению.
Если ни один из элементов не удовлетворяет выражению или само выражение составлено некорректно, то функция возвращает false.
Примечание: в jQuery 1.3 поддерживаются все типы выражений. Ранее все сложные выражения, например, такие, которые содержали селекторы +, ~ и >, всегда возвращали true.
filter используется внутренне, поэтому все правила, которые применимы там, применимы и здесь.
Аргументы:
| выражение |
Строка |
|
| Выражение, с помощью которого необходимо отфильтровывать элементы. |
Примеры:
| Name |
Type |
Несколько способов использования is() внутри обработчика событий.
$("div").one('click', function () {
if ($(this).is(":first-child")) {
$("p").text("It's the first div.");
} else if ($(this).is(".blue,.red")) {
$("p").text("It's a blue or red div.");
} else if ($(this).is(":contains('Peter')")) {
$("p").text("It's Peter!");
} else {
$("p").html("It's nothing <em>special</em>.");
}
$("p").hide().slideDown("slow");
$(this).css({"border-style": "inset", cursor:"default"});
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("div").one('click', function () {
if ($(this).is(":first-child")) {
$("p").text("It's the first div.");
} else if ($(this).is(".blue,.red")) {
$("p").text("It's a blue or red div.");
} else if ($(this).is(":contains('Peter')")) {
$("p").text("It's Peter!");
} else {
$("p").html("It's nothing <em>special</em>.");
}
$("p").hide().slideDown("slow");
$(this).css({"border-style": "inset", cursor:"default"});
});
});
</script>
<style>
div { width:60px; height:60px; margin:5px; float:left;
border:4px outset; background:green; text-align:center;
font-weight:bolder; cursor:pointer; }
.blue { background:blue; }
.red { background:red; }
span { color:white; font-size:16px; }
p { color:red; font-weight:bolder; background:yellow;
margin:3px; clear:left; display:none; }
</style>
</head>
<body>
<div></div>
<div class="blue"></div>
<div></div>
<div class="red"></div>
<div><br/><span>Peter</span></div>
<div class="blue"></div>
<p> </p>
</body>
</html>
Возвращает true, так как родительский объект элемента ввода input является элементом формы.
var isFormParent = $("input[@type='checkbox']").parent().is("form")
$("div").text("isFormParent = " + isFormParent);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
var isFormParent = $("input[@type='checkbox']").parent().is("form")
$("div").text("isFormParent = " + isFormParent);
});
</script>
<style>div { color:red; }</style>
</head>
<body>
<form><input type="checkbox" /></form>
<div></div>
</body>
</html>
Возвращает false, так как родительский объект элемента ввода input является элементом блока <p>.
var isFormParent = $("input[@type='checkbox']").parent().is("form")
$("div").text("isFormParent = " + isFormParent);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
var isFormParent = $("input[@type='checkbox']").parent().is("form")
$("div").text("isFormParent = " + isFormParent);
});
</script>
<style>div { color:red; }</style>
</head>
<body>
<form><p><input type="checkbox" /></p></form>
<div></div>
</body>
</html>