mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-29 20:20:27 +00:00
142 lines
No EOL
5.7 KiB
HTML
142 lines
No EOL
5.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1"><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
|
|
<div data-role="page">
|
|
<div data-role="header">
|
|
<h1>:nth-child Selector</h1>
|
|
|
|
</div>
|
|
<div data-role="content" data-theme="c" id="nth-child1">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">nth-child</span> selector</h2>
|
|
<div class=" entry-details">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/version/1.1.4/">1.1.4</a></span>jQuery(':nth-child(index/even/odd/equation)')</h4>
|
|
<ul class="signatures"><li><dl class="arguments">
|
|
<dt>index</dt>
|
|
<dd>The index of each child to match, starting with <code>1</code>, the string <code>even</code> or <code>odd</code>, or an equation ( eg. <code>:nth-child(even)</code>, <code>:nth-child(4n)</code> )</dd>
|
|
</dl></li></ul>
|
|
<p class="desc"><strong>Description: </strong>Selects all elements that are the nth-child of their parent.</p>
|
|
<div class="longdesc">
|
|
<p>Because jQuery's implementation of <code>:nth-child(n)</code> is strictly derived from the CSS specification, the value of <code>n</code> is "1-indexed", meaning that the counting starts at 1. For all other selector expressions, however, jQuery follows JavaScript's "0-indexed" counting. Therefore, given a single <code><ul></code> containing two <code><li></code>s, <code>$('li:nth-child(1)')</code> selects the first <code><li></code> while <code>$('li:eq(1)')</code> selects the second.</p>
|
|
<p>The <code>:nth-child(n)</code> pseudo-class is easily confused with <code>:eq(n)</code>, even though the two can result in dramatically different matched elements. With <code>:nth-child(n)</code>, all children are counted, regardless of what they are, and the specified element is selected only if it matches the selector attached to the pseudo-class. With <code>:eq(n)</code> only the selector attached to the pseudo-class is counted, not limited to children of any other element, and the nth one is selected.</p>
|
|
<p>Further discussion of this unusual usage can be found in the <a href="http://www.w3.org/TR/css3-selectors/#nth-child-pseudo">W3C CSS specification</a>.</p>
|
|
</div>
|
|
<h3>Examples:</h3>
|
|
<div id="entry-examples" class="entry-examples">
|
|
<div id="example-0">
|
|
<h4>Example: <span class="desc">Finds the second li in each matched ul and notes it.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
|
|
div { float:left; }
|
|
span { color:blue; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<div><ul>
|
|
<li>John</li>
|
|
<li>Karl</li>
|
|
<li>Brandon</li>
|
|
|
|
</ul></div>
|
|
<div><ul>
|
|
<li>Sam</li>
|
|
</ul></div>
|
|
|
|
<div><ul>
|
|
<li>Glen</li>
|
|
<li>Tane</li>
|
|
<li>Ralph</li>
|
|
|
|
<li>David</li>
|
|
</ul></div>
|
|
<script>$("ul li:nth-child(2)").append("<span> - 2nd!</span>");</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-1">
|
|
<h4>Example: <span class="desc">This is a playground to see how the selector works with different strings. Notice that this is different from the :even and :odd which have no regard for parent and just filter the list of elements to every other one. The :nth-child, however, counts the index of the child to its particular parent. In any case, it's easier to see than explain so...</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
button { display:block; font-size:12px; width:100px; }
|
|
div { float:left; margin:10px; font-size:10px;
|
|
border:1px solid black; }
|
|
span { color:blue; font-size:18px; }
|
|
#inner { color:red; }
|
|
td { width:50px; text-align:center; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<div>
|
|
<button>:nth-child(even)</button>
|
|
<button>:nth-child(odd)</button>
|
|
<button>:nth-child(3n)</button>
|
|
|
|
<button>:nth-child(2)</button>
|
|
</div>
|
|
<div>
|
|
<button>:nth-child(3n+1)</button>
|
|
<button>:nth-child(3n+2)</button>
|
|
|
|
<button>:even</button>
|
|
<button>:odd</button>
|
|
</div>
|
|
<div><table>
|
|
|
|
<tr><td>John</td></tr>
|
|
<tr><td>Karl</td></tr>
|
|
<tr><td>Brandon</td></tr>
|
|
|
|
<tr><td>Benjamin</td></tr>
|
|
</table></div>
|
|
<div><table>
|
|
<tr><td>Sam</td></tr>
|
|
|
|
</table></div>
|
|
<div><table>
|
|
<tr><td>Glen</td></tr>
|
|
<tr><td>Tane</td></tr>
|
|
|
|
<tr><td>Ralph</td></tr>
|
|
<tr><td>David</td></tr>
|
|
<tr><td>Mike</td></tr>
|
|
|
|
<tr><td>Dan</td></tr>
|
|
</table></div>
|
|
<span>
|
|
tr<span id="inner"></span>
|
|
|
|
</span>
|
|
<script>
|
|
$("button").click(function () {
|
|
var str = $(this).text();
|
|
$("tr").css("background", "white");
|
|
$("tr" + str).css("background", "#ff0000");
|
|
$("#inner").text(str);
|
|
});
|
|
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body></html> |