mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-17 14:30:28 +00:00
59 lines
No EOL
2.6 KiB
HTML
59 lines
No EOL
2.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head>
|
|
<meta charset="utf-8" /><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
|
|
<div data-role="page">
|
|
<div data-role="header">
|
|
<h1>jQuery.inArray()</h1>
|
|
|
|
</div>
|
|
<div data-role="content" data-theme="c" id="jQuery.inArray1">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">jQuery.inArray( value, array )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#Number">Number</a></span>
|
|
</h2>
|
|
<div class=" entry-details">
|
|
<p class="desc"><strong>Description: </strong>Search for a specified value within an array and return its index (or -1 if not found).</p>
|
|
<ul class="signatures"><li class="signature" id="jQuery.inArray-value-array">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.2/">1.2</a></span>jQuery.inArray( value, array )</h4>
|
|
<p class="arguement"><strong>value</strong>The value to search for.</p>
|
|
<p class="arguement"><strong>array</strong>An array through which to search.</p>
|
|
</li></ul>
|
|
<div class="longdesc">
|
|
<p>The <code>$.inArray()</code> method is similar to JavaScript's native <code>.indexOf()</code> method in that it returns -1 when it doesn't find a match. If the first element within the array matches <code>value</code>, <code>$.inArray()</code> returns 0.</p>
|
|
<p>Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of <code>value</code> within <code>array</code>, we need to check if it's not equal to (or greater than) -1.</p>
|
|
</div>
|
|
<h3>Example:</h3>
|
|
<div id="entry-examples" class="entry-examples"><div id="example-0">
|
|
<h4><span class="desc">Report the index of some elements in the array.</span></h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
div { color:blue; }
|
|
span { color:red; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<div>"John" found at <span></span></div>
|
|
<div>4 found at <span></span></div>
|
|
<div>"Karl" not found, so <span></span></div>
|
|
<script>var arr = [ 4, "Pete", 8, "John" ];
|
|
|
|
$("span:eq(0)").text(jQuery.inArray("John", arr));
|
|
$("span:eq(1)").text(jQuery.inArray(4, arr));
|
|
$("span:eq(2)").text(jQuery.inArray("Karl", arr));
|
|
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body></html> |