JavaScript DHTML/jQuery/Selector children
Содержание
- 1 Finds all children of the element with id "main"
- 2 Finds all p elements that are children of a div element
- 3 Is it the first child
- 4 Matches all child elements specified by child of elements specified by parent
- 5 nthChild(index)
- 6 onlyChild(): If the parent has other child elements, nothing is matched.
- 7 Select n-th child
Finds all children of the element with id "main"
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#main > *").css("border", "3px solid red");
});
</script>
</head>
<body>
<body>
<span id="main">
<div>asdf</div>
<button>Child</button>
<span>A Span</span>
</span>
</body>
</html>
Finds all p elements that are children of a div element
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div > p").css("border", "1px solid gray");
});
</script>
</head>
<body>
<body>
<div><p>asdf</p></div>
</body>
</html>
Is it the first child
<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("It"s the first div.");
}else{
$("p").text("It"s NOT the first div.");
}
$("p").hide().slideDown("slow");
});
});
</script>
<style>
div { border:2px white solid;}
</style>
</head>
<body>
<body>
Press each to see the text.
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<div>asdf</div>
<p></p>
</body>
</html>
Matches all child elements specified by child of elements specified by parent
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#main > *").css("border", "1px solid red");
});
</script>
</head>
<body>
<span id="main">
<button>Child</button>
<form>
<button>grand child</button>
</form>
</span>
</body>
</html>
nthChild(index)
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("ul li:nth-child(2)").append("<span> - 2nd!</span>");
});
</script>
</head>
<body>
<body>
<div>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</div>
<div>
<ul>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
</ul>
</div>
<div>
<ul>
<li>H</li>
<li>I</li>
<li>J</li>
<li>K</li>
</ul>
</div>
</body>
</html>
onlyChild(): If the parent has other child elements, nothing is matched.
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div ul:only-child").text("only has ul").css("border", "2px blue solid");
});
</script>
</head>
<body>
<body>
<div>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</div>
<div>
<ul>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
</ul>
</div>
<div>
<ul>
<li>H</li>
<li>I</li>
<li>J</li>
<li>K</li>
</ul>
</div>
</body>
</html>
Select n-th child
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("ul li:nth-child(2)").append("<span>*</span>");
});
</script>
</head>
<body>
<div>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</div>
</body>
</html>