Пример:
Выбрать все элементы 'дети' для элемента по которому произошёл щелчок мыши.
"jQuery"
$("#container").click(function (e) {
$("*").removeClass("hilite");
var $kids = $(e.target).children();
var len = $kids.addClass("hilite").length;
$("#results span:first").text(len);
$("#results span:last").text(e.target.tagName);
e.preventDefault();
return false;
});
"HTML"
div id="container">
<div>
This <span>is the <em>way</em> we</span>
write <em>the</em> demo,
</div>
<div>
<b>w</b>rit<b>e</b> the <span>demo,</span> <button>write
the</button> demo,
</div>
<div>
This <span>the way we <em>write</em> the <em>demo</em> so</span>
<input type="text" value="early" /> in
</div>
<span>t</span>he <span>m</span>orning.
<span id="results">Found <span>0</span> children in <span>TAG</span>.</span>
</div>
"CSS"
body { font-size:16px; font-weight:bolder; }
div { width:130px; height:82px; margin:10px; float:left;
border:1px solid blue; padding:4px; }
#container { width:auto; height:105px; margin:0; float:none;
border:none; }
.hilite { border-color:red; }
#results { display:block; color:red; }
p { margin:10px; border:1px solid transparent; }
span { color:blue; border:1px solid transparent; }
input { width:100px; }
em { border:1px solid transparent; }
a { border:1px solid transparent; }
b { border:1px solid transparent; }
button { border:1px solid transparent; }
"Живой пример jQuery"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="http://slyweb.ru/css/jqueryiframe.css"
rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#container").click(function (e) {
$("*").removeClass("hilite");
var $kids = $(e.target).children();
var len = $kids.addClass("hilite").length;
$("#results span:first").text(len);
$("#results span:last").text(e.target.tagName);
e.preventDefault();
return false;
});
});
</script>
</head>
<body class="iframe">
div id="container">
<div>
This <span>is the <em>way</em> we</span>
write <em>the</em> demo,
</div>
<div>
<b>w</b>rit<b>e</b> the <span>demo,</span> <button>write
the</button> demo,
</div>
<div>
This <span>the way we <em>write</em> the <em>demo</em> so</span>
<input type="text" value="early" /> in
</div>
<span>t</span>he <span>m</span>orning.
<span id="results">Found <span>0</span> children in <span>TAG</span>.</span>
</div>
</body>
</html>
<style>
body { font-size:16px; font-weight:bolder; }
div { width:130px; height:82px; margin:10px; float:left;
border:1px solid blue; padding:4px; }
#container { width:auto; height:105px; margin:0; float:none;
border:none; }
.hilite { border-color:red; }
#results { display:block; color:red; }
p { margin:10px; border:1px solid transparent; }
span { color:blue; border:1px solid transparent; }
input { width:100px; }
em { border:1px solid transparent; }
a { border:1px solid transparent; }
b { border:1px solid transparent; }
button { border:1px solid transparent; }
</style>
Пример:
Найти все элементы "дети" в каждом из элементов div.
Пример:
Найти все дочерние элементы в каждом из элементов div.
"HTML"
Hello (this is a paragraph)
<div><span>Hello Again (this span is a child of the a div)</span></div>And <span>Again</span> (in another paragraph)
<div>And One Last <span>Time</span> (most text directly in a div)</div>
"CSS"
body { font-size:16px; font-weight:bolder; }
span { color:blue; }
p { margin:5px 0; }
"Живой пример jQuery"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="http://slyweb.ru/css/jqueryiframe.css"
rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").children().css("border-bottom", "3px double red");
});
</script>
</head>
<body class="iframe">
Hello (this is a paragraph)
<div><span>Hello Again (this span is a child of the a div)</span></div>
And <span>Again</span> (in another paragraph)
<div>And One Last <span>Time</span> (most text directly in a div)</div>
</body>
</html>
<style>
body { font-size:16px; font-weight:bolder; }
span { color:blue; }
p { margin:5px 0; }
</style>
Пример:
Найти все элементы "дети" с классом "selected" для каждого элемента div.
"HTML"
<div> <span>Hello</span> <p class="selected">Hello Again</p> <div class="selected">And Again</div>And One Last Time
</div>
"CSS"
body { font-size:16px; font-weight:bolder; }
p { margin:5px 0; }
"Живой пример jQuery"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="http://slyweb.ru/css/jqueryiframe.css"
rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div").children(".selected").css("color", "blue");
});
</script>
</head>
<body class="iframe">
<div>
<span>Hello</span>
<p class="selected">Hello Again</p>
<div class="selected">And Again</div>
And One Last Time
</div>
</body>
</html>
<style>
body { font-size:16px; font-weight:bolder; }
p { margin:5px 0; }
</style>